package com.engine.qz.service.impl;

import com.engine.core.impl.Service;
import com.engine.kq.biz.KQHolidaySetBiz;
import com.engine.qz.service.CommissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2025/1/6
 * @Description 提成
 * @Author AdminZm
 */
public class CommissionServiceImpl extends Service implements CommissionService {

    BaseBean baseBean = new BaseBean();

    @Override
    public Map<String, Object> generateCommissionPlan(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        baseBean.writeLog("generateCommissionPlan start:" + params);
        try {
            String ny = Util.null2String(params.get("ny"));
            baseBean.writeLog("generateCommissionPlan ny:" + ny);
            if (StringUtils.isEmpty(ny)) {
                baseBean.writeLog("generateCommissionPlan ny is null.");
                result.put("mes", "年月不可为空！");
                result.put("code", "400");
                return result;
            }

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            YearMonth yearMonth = YearMonth.parse(ny, formatter);
            // 获取该月的最后一天
            LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
            String firstDay = ny + "-01";
            String lastDay = lastDayOfMonth.toString();

            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            // 先删除该年月的数据
            rs.execute("DELETE FROM uf_ygtcfa WHERE szny = '" + ny + "'");

            // 岗位fieldId 3
            String gwFieldId = rs.getPropValue("qz_tc_zm_prop", "gw_field_id_tc");
            // 获取人员对应分部、部门、聚才林岗位
            Map<String, String> fbMap = new HashMap<>();
            Map<String, String> bmMap = new HashMap<>();
            Map<String, String> gwMap = new HashMap<>();
            Map<String, String> ghMap = new HashMap<>();
            rs.execute("SELECT a.id,a.subcompanyid1,a.departmentid,a.workcode, b." + gwFieldId + " FROM hrmresource a LEFT JOIN cus_fielddata b " +
                    " ON a.id = b.id AND b.scope = 'HrmCustomFieldByInfoType' AND b.scopeid = 3 ");
            while (rs.next()) {
                String ryIdT = rs.getString("id");
                String subcompanyid1 = rs.getString("subcompanyid1");
                String departmentid = rs.getString("departmentid");
                String workcode = rs.getString("workcode");
                String jclGw = rs.getString(gwFieldId);
                if (StringUtils.isEmpty(ryIdT)) {
                    continue;
                }
                fbMap.put(ryIdT, subcompanyid1);
                bmMap.put(ryIdT, departmentid);
                gwMap.put(ryIdT, jclGw);
                ghMap.put(ryIdT, workcode);
            }

            // 员工提成建模id
            String modeId = rs.getPropValue("qz_tc_zm_prop", "ygtc_mode_id");
            // 员工提成查询id
            String cusId = rs.getPropValue("qz_tc_zm_prop", "ygtc_cus_id");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
            List<String> strList = new ArrayList<>();
            List<List> insertParams = new ArrayList<>();
            rs.execute("SELECT id,ryfwbdsql FROM uf_tcfasz");
            while (rs.next()) {
                String id = rs.getString("id");
                String ryfwbdsqlT = rs.getString("ryfwbdsql");
                if (StringUtils.isEmpty(ryfwbdsqlT) || StringUtils.isEmpty(id)) {
                    continue;
                }
                String ryfwbdsql = ryfwbdsqlT.replace("$ksrq$", firstDay).replace("$jsrq$", lastDay);
                baseBean.writeLog("generateCommissionPlan ryfwbdsql:" + id + "、" + ryfwbdsql);

                rs1.execute(ryfwbdsql);
                while (rs1.next()) {
                    String ryId = rs1.getString(1);
                    if (StringUtils.isNotEmpty(ryId)) {
                        String szfb = fbMap.get(ryId);
                        if (StringUtils.isEmpty(szfb)) {
                            szfb = "0";
                        }
                        String szbm = bmMap.get(ryId);
                        if (StringUtils.isEmpty(szbm)) {
                            szbm = "0";
                        }
                        String gh = ghMap.get(ryId);
                        if (StringUtils.isEmpty(gh)) {
                            gh = "0";
                        }
                        List insertParam = new ArrayList<>();
                        insertParam.add(id);
                        insertParam.add(ryId);
                        insertParam.add(ny);
                        insertParam.add(szfb);
                        insertParam.add(szbm);
                        insertParam.add(gh);
                        insertParam.add(modeId);
                        insertParam.add(cusId);
                        insertParam.add("1");
                        insertParam.add("0");
                        insertParam.add(sdfDate.format(new Date()));
                        insertParam.add(sdfTime.format(new Date()));
                        insertParams.add(insertParam);
                    }
                }
            }

            if (CollectionUtils.isEmpty(insertParams)) {
                baseBean.writeLog("generateCommissionPlan insertParams is null.");
            } else {
                baseBean.writeLog("generateCommissionPlan insertParams size:" + insertParams.size());
                String insertSql = "INSERT INTO uf_ygtcfa (tcfa, yg, szny, ygfb, ygbm, yggh, formmodeid,MODEUUID, modedatacreater," +
                        "modedatacreatertype, modedatacreatedate, modedatacreatetime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
                rs.executeBatchSql(insertSql, insertParams);
            }

            // 权限重构
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setNewRight(true);
            modeRightInfo.editModeDataShare(1, Integer.parseInt(modeId), Integer.parseInt(cusId));
        } catch (Exception e) {
            baseBean.writeLog("generateCommissionPlan error:" + e);
            result.put("mes", "系统错误，请联系管理员！");
            result.put("code", "400");
            return result;
        }
        baseBean.writeLog("generateCommissionPlan end.");
        result.put("mes", "正在生成，请稍等！");
        result.put("code", "200");
        return result;
    }

    @Override
    public Map<String, Object> countCommission(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        baseBean.writeLog("countCommission start:" + params);
        try {
            String ryId = Util.null2String(params.get("ryid"));
            String faId = Util.null2String(params.get("tcfa"));
            if (StringUtils.isEmpty(ryId) && StringUtils.isEmpty(faId)) {
                baseBean.writeLog("countCommission param is null.");
                result.put("mes", "请至少选择方案或人员！");
                result.put("code", "400");
                return result;
            }
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            Map<String, String> oldDataMap = new HashMap<>();
            String sql1 = "SELECT * FROM uf_yjtcjs WHERE 1 = 1 ";
            if (StringUtils.isNotEmpty(ryId)) {
                sql1 = sql1 + " AND yg in (" + ryId + ") ";
            }
            if (StringUtils.isNotEmpty(faId)) {
                sql1 = sql1 + " AND tcfa in (" + faId + ") ";
            }
            rs.execute(sql1);
            while (rs.next()) {
                String id = rs.getString("id");
                String khfa = rs.getString("tcfa");
                String yg = rs.getString("yg");
                String szny = rs.getString("szny");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(khfa) || StringUtils.isEmpty(yg) || StringUtils.isEmpty(szny)) {
                    continue;
                }
                oldDataMap.put(yg + "_" + khfa + "_" + szny, id);
            }

            Map<String, List<Map<String, String>>> dt1ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt1");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String pjqjkszl = rs.getString("pjqjkszl");
                String pjqjjszl = rs.getString("pjqjjszl");
                String tcxszl = rs.getString("tcxszl");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("pjqjkszl", pjqjkszl);
                map.put("pjqjjszl", pjqjjszl);
                map.put("tcxszl", tcxszl);
                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt1ListMap)) && dt1ListMap.containsKey(mainid)) {
                    mapList = dt1ListMap.get(mainid);
                }
                mapList.add(map);
                dt1ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt2ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt2");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String pjqjkspt = rs.getString("pjqjkspt");
                String pjqjjspt = rs.getString("pjqjjspt");
                String tcxspt = rs.getString("tcxspt");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("pjqjkspt", pjqjkspt);
                map.put("pjqjjspt", pjqjjspt);
                map.put("tcxspt", tcxspt);
                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt2ListMap)) && dt2ListMap.containsKey(mainid)) {
                    mapList = dt2ListMap.get(mainid);
                }
                mapList.add(map);
                dt2ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt3ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt3");
            while (rs.next()) {
                String dtId = rs.getString("id");
                String mainid = rs.getString("mainid");
                String fgslbjl = rs.getString("fgslbjl");
                String jebzjl = rs.getString("jebzjl");
                String dbrs = rs.getString("dbrs");
                String dbjljl = rs.getString("dbjljl");
                String yxj = rs.getString("yxj");
                if (StringUtils.isEmpty(mainid) || StringUtils.isEmpty(dtId)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("dtId", dtId);
                map.put("fgslbjl", fgslbjl);
                map.put("jebzjl", jebzjl);
                map.put("dbrs", dbrs);
                map.put("dbjljl", dbjljl);
                map.put("yxj", yxj);
                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt3ListMap)) && dt3ListMap.containsKey(mainid)) {
                    mapList = dt3ListMap.get(mainid);
                }
                mapList.add(map);
                dt3ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt4ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt4");
            while (rs.next()) {
                String dtId = rs.getString("id");
                String mainid = rs.getString("mainid");
                String fgslbzj = rs.getString("fgslbzj");
                String jebzzj = rs.getString("jebzzj");
                String dbtds = rs.getString("dbtds");
                String dbjlzj = rs.getString("dbjlzj");
                String yxj = rs.getString("yxj");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("dtId", dtId);
                map.put("fgslbzj", fgslbzj);
                map.put("jebzzj", jebzzj);
                map.put("dbtds", dbtds);
                map.put("dbjlzj", dbjlzj);
                map.put("yxj", yxj);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt4ListMap)) && dt4ListMap.containsKey(mainid)) {
                    mapList = dt4ListMap.get(mainid);
                }
                mapList.add(map);
                dt4ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt5ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt5");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String jebzfs = rs.getString("jebzfs");
                String dbfss = rs.getString("dbfss");
                String dbjlfs = rs.getString("dbjlfs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("jebzfs", jebzfs);
                map.put("dbfss", dbfss);
                map.put("dbjlfs", dbjlfs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt5ListMap)) && dt5ListMap.containsKey(mainid)) {
                    mapList = dt5ListMap.get(mainid);
                }
                mapList.add(map);
                dt5ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt6ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt6");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String fgslbmb = rs.getString("fgslbmb");
                String jebzmb = rs.getString("jebzmb");
                String dbjlmb = rs.getString("dbjlmb");
                String yxj = rs.getString("yxj");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("fgslbmb", fgslbmb);
                map.put("jebzmb", jebzmb);
                map.put("dbjlmb", dbjlmb);
                map.put("yxj", yxj);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt6ListMap)) && dt6ListMap.containsKey(mainid)) {
                    mapList = dt6ListMap.get(mainid);
                }
                mapList.add(map);
                dt6ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt7ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt7");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String mczh = rs.getString("mczh");
                String jezh = rs.getString("jezh");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("mczh", mczh);
                map.put("jezh", jezh);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt7ListMap)) && dt7ListMap.containsKey(mainid)) {
                    mapList = dt7ListMap.get(mainid);
                }
                mapList.add(map);
                dt7ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt8ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt8");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String fslb = rs.getString("fslb");
                String dbjl = rs.getString("dbjl");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("dbjl", dbjl);
                map.put("fslb", fslb);
                map.put("yxj", yxj);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt8ListMap)) && dt8ListMap.containsKey(mainid)) {
                    mapList = dt8ListMap.get(mainid);
                }
                mapList.add(map);
                dt8ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt9ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt9");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String fslb = rs.getString("fslb");
                String wcz = rs.getString("wcz");
                String jjxs = rs.getString("jjxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("wcz", wcz);
                map.put("fslb", fslb);
                map.put("yxj", yxj);
                map.put("jjxs", jjxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt9ListMap)) && dt9ListMap.containsKey(mainid)) {
                    mapList = dt9ListMap.get(mainid);
                }
                mapList.add(map);
                dt9ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt10ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt10");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String khbms = rs.getString("khbms");
                String dyxs = rs.getString("dyxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("khbms", khbms);
                map.put("dyxs", dyxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt10ListMap)) && dt10ListMap.containsKey(mainid)) {
                    mapList = dt10ListMap.get(mainid);
                }
                mapList.add(map);
                dt10ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt11ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt11");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String pjmc = rs.getString("pjmc");
                String pjqjks = rs.getString("pjqjks");
                String pjqjjsh = rs.getString("pjqjjsh");
                String tcxs = rs.getString("tcxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("yxj", yxj);
                map.put("pjmc", pjmc);
                map.put("pjqjks", pjqjks);
                map.put("pjqjjsh", pjqjjsh);
                map.put("tcxs", tcxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt11ListMap)) && dt11ListMap.containsKey(mainid)) {
                    mapList = dt11ListMap.get(mainid);
                }
                mapList.add(map);
                dt11ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt12ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt12");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String dyfbbhxj = rs.getString("dyfbbhxj");
                String fgqyssbl = rs.getString("fgqyssbl");
                String jlrbl = rs.getString("jlrbl");
                String jlrwfssbl = rs.getString("jlrwfssbl");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("yxj", yxj);
                map.put("dyfbbhxj", dyfbbhxj);
                map.put("fgqyssbl", fgqyssbl);
                map.put("jlrbl", jlrbl);
                map.put("jlrwfssbl", jlrwfssbl);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt12ListMap)) && dt12ListMap.containsKey(mainid)) {
                    mapList = dt12ListMap.get(mainid);
                }
                mapList.add(map);
                dt12ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt13ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt13");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String pjqjks = rs.getString("pjqjks");
                String pjqjjs = rs.getString("pjqjjs");
                String tcxs = rs.getString("tcxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("yxj", yxj);
                map.put("pjqjks", pjqjks);
                map.put("pjqjjs", pjqjjs);
                map.put("tcxs", tcxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt13ListMap)) && dt13ListMap.containsKey(mainid)) {
                    mapList = dt13ListMap.get(mainid);
                }
                mapList.add(map);
                dt13ListMap.put(mainid, mapList);
            }

            // 获取分部对应的所有上级分部
            Map<String, String> fbAndSupMap = new HashMap<>();
            rs.execute("SELECT id, supsubcomid FROM hrmsubcompany WHERE canceled != 1 or canceled is null");
            while (rs.next()) {
                String fbId = rs.getString("id");
                if (StringUtils.isEmpty(fbId)) {
                    continue;
                }
                String supsubcomid = rs.getString("supsubcomid");
                fbAndSupMap.put(fbId, supsubcomid);
            }
            Map<String, Set<String>> fbAndXjListMap = new HashMap<>();
            for (Map.Entry<String, String> entry : fbAndSupMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (StringUtils.isNotEmpty(key)) {
                    Set<String> tempSet = new HashSet<>();
                    if ((!CollectionUtils.isEmpty(fbAndXjListMap)) && fbAndXjListMap.containsKey(key)) {
                        tempSet = fbAndXjListMap.get(key);
                    }
                    tempSet.add(key);
                    fbAndXjListMap.put(key, tempSet);
                }
                if (StringUtils.isNotEmpty(value)) {
                    Set<String> tempSet = new HashSet<>();
                    if ((!CollectionUtils.isEmpty(fbAndXjListMap)) && fbAndXjListMap.containsKey(value)) {
                        tempSet = fbAndXjListMap.get(value);
                    }
                    tempSet.add(value);
                    tempSet.add(key);
                    fbAndXjListMap.put(value, tempSet);
                }
            }

            // 获取公司对应部门数
            Map<String, Map<String, String>> gsAndBmsMapMap = new HashMap<>();
            rs.execute("SELECT szny, dyfs, khbms FROM uf_khbms");
            while (rs1.next()) {
                String dyfsTemp = rs.getString("dyfs");
                String sznyTemp = rs.getString("szny");
                if (StringUtils.isEmpty(dyfsTemp) || StringUtils.isEmpty(sznyTemp)) {
                    continue;
                }
                String khbmsTemp = rs.getString("khbms");
                Map<String, String> gsAndBmsMap = new HashMap<>();
                if ((!CollectionUtils.isEmpty(gsAndBmsMapMap)) && gsAndBmsMapMap.containsKey(sznyTemp)) {
                    gsAndBmsMap = gsAndBmsMapMap.get(sznyTemp);
                }
                gsAndBmsMap.put(dyfsTemp, khbmsTemp);
                gsAndBmsMapMap.put(sznyTemp, gsAndBmsMap);
            }

            // 获取扶贫金
            Map<String, String> fpjMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_fpj ");
            while (rs.next()) {
                String yg = rs.getString("yg");
                String sztcfa = rs.getString("sztcfa");
                String sznyFpj = rs.getString("szny");
                String jeFpj = rs.getString("je");
                if (StringUtils.isEmpty(yg) || StringUtils.isEmpty(sztcfa) || StringUtils.isEmpty(sznyFpj)) {
                    continue;
                }
                fpjMap.put(yg + "_" + sztcfa + "_" + sznyFpj, jeFpj);
            }

            // 区域招商经理服务部门数及业绩比例
            Map<String, String> qyzsjlbmyjMap = new HashMap<>();
            Map<String, String> qyzsjlbmyjBlMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_qyzsjlfwbmsyjbl ");
            while (rs.next()) {
                String yg = rs.getString("yg");
                String sztcfa = rs.getString("szkhfa");
                String sznyBmyj = rs.getString("szny");
                String fwbms = rs.getString("fwbms");
                String fsthssbl = rs.getString("fsthssbl");
                if (StringUtils.isEmpty(yg) || StringUtils.isEmpty(sztcfa) || StringUtils.isEmpty(sznyBmyj)) {
                    continue;
                }
                qyzsjlbmyjMap.put(yg + "_" + sztcfa + "_" + sznyBmyj, fwbms);
                qyzsjlbmyjBlMap.put(yg + "_" + sztcfa + "_" + sznyBmyj, fsthssbl);
            }

            // “净新签实收完成”指标编码
            String jxqsswcIndexCode = rs.getPropValue("qz_tc_zm_prop", "jxqsswc_index_code");
            // 获取岗位名称为“销售经理”的人员id
            // 岗位fieldId 3
            String gwFieldId = rs.getPropValue("qz_tc_zm_prop", "gw_field_id_tc");
            Set<String> gwSet = new HashSet<>();
            rs.execute("SELECT b.id,b." + gwFieldId + ",c.gwmc FROM cus_fielddata b LEFT JOIN uf_JCL_GWGL c " +
                    " ON b." + gwFieldId + " = c.id WHERE b.scope = 'HrmCustomFieldByInfoType' AND b.scopeid = 3 AND b." + gwFieldId
                    + " is not null AND c.gwmc = '销售经理'");
            while (rs.next()) {
                String id = rs.getString("id");
                String gwmc = rs.getString("gwmc");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(gwmc) || !StringUtils.equals(gwmc, "销售经理")) {
                    continue;
                }
                gwSet.add(id);
            }

            String today = DateUtil.getCurrentDate();

            // 获取当前生效的分司以及对应级别
            Map<String, String> fbDjMap = new HashMap<>();
            rs.execute("SELECT fb, lb\n" +
                    "FROM uf_yjtl\n" +
                    "WHERE shenxrq <= '" + today + "' AND sxrq >= '" + today + "'\n" +
                    "ORDER BY CONVERT(DATE, shenxrq, 120) DESC");
            while (rs.next()) {
                String fb = rs.getString("fb");
                String lb = rs.getString("lb");
                if (StringUtils.isEmpty(fb) || StringUtils.isEmpty(lb)) {
                    continue;
                }
                fbDjMap.put(fb, lb);
            }

            // 获取BI系统中间表结果映射
            Map<String, String> yjMap = new HashMap<>();
            Map<String, String> fsYjMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_BIxtzjb ");
            while (rs.next()) {
                String yg = rs.getString("yg");
                String gh = rs.getString("gh");
                String szzq = rs.getString("szzq");
                String bizbxm = rs.getString("zbxm");
                if (StringUtils.isEmpty(szzq) || StringUtils.isEmpty(bizbxm)) {
                    continue;
                }
                String wcz = rs.getString("wcz");
                if (StringUtils.isNotEmpty(yg)) {
                    yjMap.put(yg + "_" + bizbxm + "_" + szzq, wcz);
                }
                if (StringUtils.isNotEmpty(gh) && StringUtils.isEmpty(yg)) {
                    fsYjMap.put(gh + "_" + bizbxm + "_" + szzq, wcz);
                }
            }

            // 获取二次折算配置
            Map<String, String> zsxsMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_TCECZSPZ ");
            while (rs.next()) {
                String xm = rs.getString("xm");
                if (StringUtils.isEmpty(xm)) {
                    continue;
                }
                String fadjxs = rs.getString("fadjxs");
                String zjdjxs = rs.getString("zjdjxs");
                String sjdjtds = rs.getString("sjdjtds");
                if (StringUtils.isEmpty(sjdjtds)) {
                    sjdjtds = "0";
                }
                String zsxs = null;
                if (StringUtils.isEmpty(fadjxs) && StringUtils.isEmpty(zjdjxs)) {
                    if (new BigDecimal(sjdjtds).compareTo(new BigDecimal("2")) > 0) {
                        zsxs = "0.85";
                    } else {
                        zsxs = "1";
                    }
                } else {
                    if (StringUtils.isNotEmpty(zjdjxs)) {
                        zsxs = zjdjxs;
                    }
                    if (StringUtils.isNotEmpty(fadjxs)) {
                        zsxs = fadjxs;
                    }
                }
                zsxsMap.put(xm, zsxs);
            }

            List<List> insertList = new ArrayList<>();
            List<List> updateList = new ArrayList<>();

            // 业绩提成建模id
            String modeId = rs.getPropValue("qz_tc_zm_prop", "yjtc_mode_id");
            // 业绩提成查询id
            String cusId = rs.getPropValue("qz_tc_zm_prop", "yjtc_cus_id");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
            SimpleDateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            // 定义一个Comparator来根据Map中key为"pjqjkszl"的value进行排序
            Comparator<Map<String, String>> comparator = Comparator.comparingInt(
                    map -> Integer.parseInt(map.getOrDefault("pjqjkszl", "0")) // 使用getOrDefault来避免NullPointerException
            );

            Comparator<Map<String, String>> comparator1 = Comparator.comparingInt(
                    map -> Integer.parseInt(map.getOrDefault("jebzfs", "0")) // 使用getOrDefault来避免NullPointerException
            );

            Comparator<Map<String, String>> comparator2 = Comparator.comparingInt(
                    map -> Integer.parseInt(map.getOrDefault("yxj", "0")) // 使用getOrDefault来避免NullPointerException
            );

            String sql = "SELECT a.tcfa, a.yg, a.szny, b.fzxsetcbl, b.jslj, b.dqzjljtfzcjrywdqzjljtfzcjrywjl, b.sjlb, " +
                    "    b.sfqyeczs, b.sfqytsgz, c.bizbbm, c.bizbmc, c1.bizbbm as lrzbbm, c1.bizbmc as lrzbmc, d.subcompanyid1, " +
                    "    d.departmentid, e." + gwFieldId + ", d.workcode " +
                    " FROM uf_ygtcfa a " +
                    " LEFT JOIN uf_tcfasz b ON a.tcfa = b.id " +
                    " LEFT JOIN uf_bizbys c ON b.sjlb = c.id " +
                    " LEFT JOIN uf_bizbys c1 ON b.dqzjljtfzcjrywdqzjljtfzcjrywjl = c1.id " +
                    " LEFT JOIN hrmresource d ON d.id = a.yg " +
                    " LEFT JOIN cus_fielddata e ON e.id = a.yg AND e.scope = 'HrmCustomFieldByInfoType' AND e.scopeid = 3 " +
                    " WHERE 1 = 1 ";
            if (StringUtils.isNotEmpty(ryId)) {
                sql = sql + " AND a.yg in (" + ryId + ") ";
            }
            if (StringUtils.isNotEmpty(faId)) {
                sql = sql + " AND a.tcfa in (" + faId + ") ";
            }
            rs.execute(sql);
            while (rs.next()) {
                String tcfa = rs.getString("tcfa");
                String yg = rs.getString("yg");
                String szny = rs.getString("szny");
                String jslj = rs.getString("jslj");
                String sjlb = rs.getString("sjlb");
                String bizbbm = rs.getString("bizbbm");
                String bizbmc = rs.getString("bizbmc");
                String szfb = rs.getString("subcompanyid1");
                String szbm = rs.getString("departmentid");
                String yggh = rs.getString("workcode");
                String szgw = rs.getString(gwFieldId);
                String sfqytsgz = rs.getString("sfqytsgz");
                String sfqyeczs = rs.getString("sfqyeczs");
                String fzxsetcbl = rs.getString("fzxsetcbl");

                String lrzbId = rs.getString("dqzjljtfzcjrywdqzjljtfzcjrywjl");
                String lrzbbm = rs.getString("lrzbbm");
                String lrzbmc = rs.getString("lrzbmc");

                if (StringUtils.isEmpty(tcfa) || StringUtils.isEmpty(yg) || StringUtils.isEmpty(szny) || StringUtils.isEmpty(jslj)
                        || StringUtils.isEmpty(sjlb)) {
                    continue;
                }
                Map<String, String> gsAndBmsMap = gsAndBmsMapMap.get(szny);

                BigDecimal tcBd = new BigDecimal("0");

                String wczjg = null;
                // 增量牌级
                if (StringUtils.equals(jslj, "0")) {
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt1ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String pjqjkszl = mapTemp.get("pjqjkszl");
                        if (StringUtils.isEmpty(pjqjkszl)) {
                            pjqjkszl = "0";
                        }
                        String pjqjjszl = mapTemp.get("pjqjjszl");
                        if (StringUtils.isEmpty(pjqjjszl)) {
                            pjqjjszl = "0";
                        }
                        String tcxszl = mapTemp.get("tcxszl");
                        if (StringUtils.isEmpty(tcxszl)) {
                            tcxszl = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(pjqjkszl)) >= 0 && yjBd.compareTo(new BigDecimal(pjqjjszl)) < 0) {
                            BigDecimal multiply = (yjBd.subtract(new BigDecimal(pjqjkszl))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        } else if (yjBd.compareTo(new BigDecimal(pjqjkszl)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjszl)) >= 0) {
                            BigDecimal multiply = (new BigDecimal(pjqjjszl).subtract(new BigDecimal(pjqjkszl))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        }
                    }
                } else if (StringUtils.equals(jslj, "1")) {   // 普通牌级
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt2ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    for (Map<String, String> mapTemp : mapList) {
                        String pjqjkspt = mapTemp.get("pjqjkspt");
                        if (StringUtils.isEmpty(pjqjkspt)) {
                            pjqjkspt = "0";
                        }
                        String pjqjjspt = mapTemp.get("pjqjjspt");
                        if (StringUtils.isEmpty(pjqjjspt)) {
                            pjqjjspt = "0";
                        }
                        String tcxspt = mapTemp.get("tcxspt");
                        if (StringUtils.isEmpty(tcxspt)) {
                            tcxspt = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(pjqjkspt)) >= 0 && yjBd.compareTo(new BigDecimal(pjqjjspt)) < 0) {
                            tcBd = yjBd.multiply(new BigDecimal(tcxspt));
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "2") || StringUtils.equals(jslj, "3")) {   // 顾问综合排名奖、经理/总监综合排名奖
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt7ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    for (Map<String, String> mapTemp : mapList) {
                        String mczh = mapTemp.get("mczh");
                        if (StringUtils.isEmpty(mczh)) {
                            continue;
                        }
                        String jezh = mapTemp.get("jezh");
                        if (StringUtils.isEmpty(jezh)) {
                            jezh = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(mczh)) == 0) {
                            tcBd = new BigDecimal(jezh);
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "4")) {   // 经理合格奖
                    List<Map<String, String>> mapList = dt3ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    Map<String, String> rsbzMap = new HashMap<>();
                    Map<String, String> jlMap = new HashMap<>();
                    Map<String, String> dbCntMap = new HashMap<>();
                    rs1.execute("select id, subcompanyid1 from hrmresource where managerid =" + yg);
                    while (rs1.next()) {
                        String id = rs1.getString("id");
                        String subcompanyid1 = rs1.getString("subcompanyid1");
                        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(subcompanyid1)) {
                            continue;
                        }
                        String yj = yjMap.get(id + "_" + bizbbm + "_" + szny);
                        if (StringUtils.isEmpty(yj)) {
                            continue;
                        }
                        for (Map<String, String> mapTemp : mapListNew) {
                            String dtId = mapTemp.get("dtId");
                            if (StringUtils.isEmpty(dtId)) {
                                continue;
                            }
                            String fgslbjl = mapTemp.get("fgslbjl");
                            if (StringUtils.isNotEmpty(fgslbjl)) {
                                String djTemp = fbDjMap.get(subcompanyid1);
                                if (StringUtils.isEmpty(djTemp)) {
                                    continue;
                                }
                                if (new BigDecimal(djTemp).compareTo(new BigDecimal(fgslbjl)) != 0) {
                                    continue;
                                }
                            }
                            String jebzjl = mapTemp.get("jebzjl");
                            if (StringUtils.isEmpty(jebzjl)) {
                                jebzjl = "0";
                            }
                            if (new BigDecimal(yj).compareTo(new BigDecimal(jebzjl)) >= 0) {
                                String dbrs = mapTemp.get("dbrs");
                                String dbjljl = mapTemp.get("dbjljl");

                                rsbzMap.put(dtId, dbrs);
                                jlMap.put(dtId, dbjljl);

                                String cntStr = dbCntMap.get(dtId);
                                if (StringUtils.isEmpty(cntStr)) {
                                    cntStr = "0";
                                }
                                dbCntMap.put(dtId, new BigDecimal(cntStr).add(new BigDecimal("1")).toString());
                                break;
                            }
                        }
                    }

                    BigDecimal temp = new BigDecimal("0");
                    for (Map.Entry<String, String> mapTemp : dbCntMap.entrySet()) {
                        String key = mapTemp.getKey();
                        String value = mapTemp.getValue();
                        if (StringUtils.isEmpty(value)) {
                            continue;
                        }
                        BigDecimal valueBd = new BigDecimal(value);
                        if (valueBd.compareTo(BigDecimal.ZERO) == 0) {
                            continue;
                        }
                        String rsbz = rsbzMap.get(key);
                        if (StringUtils.isEmpty(rsbz)) {
                            rsbz = "0";
                        }
                        if (valueBd.compareTo(new BigDecimal(rsbz)) >= 0) {
                            temp = temp.add(valueBd);
                            String jl = jlMap.get(key);
                            if (StringUtils.isEmpty(jl)) {
                                jl = "0";
                            }
                            tcBd = tcBd.add(new BigDecimal(jl));
                        }
                    }
                    wczjg = temp.toString();
                } else if (StringUtils.equals(jslj, "5")) {   // 总监/分总合格奖
                    List<Map<String, String>> mapList = dt4ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    Map<String, String> rsbzMap = new HashMap<>();
                    Map<String, String> jlMap = new HashMap<>();
                    Map<String, String> dbCntMap = new HashMap<>();
                    rs1.execute("select id, subcompanyid1 from hrmresource where managerid =" + yg);
                    while (rs1.next()) {
                        String id = rs1.getString("id");
                        String subcompanyid1 = rs1.getString("subcompanyid1");
                        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(subcompanyid1)) {
                            continue;
                        }
                        String yj = yjMap.get(id + "_" + bizbbm + "_" + szny);
                        if (StringUtils.isEmpty(yj)) {
                            continue;
                        }
                        for (Map<String, String> mapTemp : mapListNew) {
                            String dtId = mapTemp.get("dtId");
                            if (StringUtils.isEmpty(dtId)) {
                                continue;
                            }
                            String fgslbjl = mapTemp.get("fgslbzj");
                            if (StringUtils.isNotEmpty(fgslbjl)) {
                                String djTemp = fbDjMap.get(subcompanyid1);
                                if (StringUtils.isEmpty(djTemp)) {
                                    continue;
                                }
                                if (new BigDecimal(djTemp).compareTo(new BigDecimal(fgslbjl)) != 0) {
                                    continue;
                                }
                            }
                            String jebzjl = mapTemp.get("jebzzj");
                            if (StringUtils.isEmpty(jebzjl)) {
                                jebzjl = "0";
                            }
                            if (new BigDecimal(yj).compareTo(new BigDecimal(jebzjl)) >= 0) {
                                String dbrs = mapTemp.get("dbtds");
                                String dbjljl = mapTemp.get("dbjlzj");

                                rsbzMap.put(dtId, dbrs);
                                jlMap.put(dtId, dbjljl);

                                String cntStr = dbCntMap.get(dtId);
                                if (StringUtils.isEmpty(cntStr)) {
                                    cntStr = "0";
                                }
                                dbCntMap.put(dtId, new BigDecimal(cntStr).add(new BigDecimal("1")).toString());
                                break;
                            }
                        }
                    }

                    BigDecimal temp = new BigDecimal("0");
                    for (Map.Entry<String, String> mapTemp : dbCntMap.entrySet()) {
                        String key = mapTemp.getKey();
                        String value = mapTemp.getValue();
                        if (StringUtils.isEmpty(value)) {
                            continue;
                        }
                        BigDecimal valueBd = new BigDecimal(value);
                        if (valueBd.compareTo(BigDecimal.ZERO) == 0) {
                            continue;
                        }
                        String rsbz = rsbzMap.get(key);
                        if (StringUtils.isEmpty(rsbz)) {
                            rsbz = "0";
                        }
                        if (valueBd.compareTo(new BigDecimal(rsbz)) >= 0) {
                            temp = temp.add(valueBd);
                            String jl = jlMap.get(key);
                            if (StringUtils.isEmpty(jl)) {
                                jl = "0";
                            }
                            tcBd = tcBd.add(new BigDecimal(jl));
                        }
                    }

                    wczjg = temp.toString();
                } else if (StringUtils.equals(jslj, "6")) {   // 盈利分司奖
                    List<Map<String, String>> mapList = dt5ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator1)
                            .collect(Collectors.toList());
                    Map<String, String> dbMap = new HashMap<>();
                    rs1.execute("select a.id,b.bifsbm from \n" +
                            "(select id from hrmsubcompany where (canceled != 1 or canceled is null) and supsubcomid = (select subcompanyid1\n" +
                            "from hrmresource where id = " + yg + ")\n" +
                            "union \n" +
                            "select subcompanyid1 as id\n" +
                            "from hrmresource where id = " + yg + ") a\n" +
                            "left join uf_BIfsyszjb b on a.id = b.dyfb");
                    while (rs1.next()) {
                        String bifsbm = rs1.getString("bifsbm");
                        if (StringUtils.isEmpty(bifsbm)) {
                            continue;
                        }
                        String yj = yjMap.get(bifsbm + "_" + bizbbm + "_" + szny);
                        if (StringUtils.isEmpty(yj)) {
                            continue;
                        }
                        for (int i = mapListNew.size(); i >= 0; i--) {
                            String jebzfs = mapListNew.get(i).get("jebzfs");
                            if (StringUtils.isEmpty(jebzfs)) {
                                continue;
                            }
                            if (new BigDecimal(yj).compareTo(new BigDecimal(jebzfs)) >= 0) {
                                String jl = dbMap.get(jebzfs);
                                if (StringUtils.isEmpty(jl)) {
                                    jl = "0";
                                }
                                dbMap.put(jebzfs, new BigDecimal(jl).add(new BigDecimal("1")).toString());
                                break;
                            }
                        }
                    }
                    BigDecimal temp = new BigDecimal("0");
                    if (!CollectionUtils.isEmpty(dbMap)) {
                        for (Map<String, String> mapTemp : mapListNew) {
                            String jebzfs = mapTemp.get("jebzfs");
                            if (StringUtils.isEmpty(jebzfs)) {
                                continue;
                            }
                            String dbfss = mapTemp.get("dbfss");
                            if (StringUtils.isEmpty(dbfss)) {
                                continue;
                            }
                            String dbjlfs = mapTemp.get("dbjlfs");
                            if (StringUtils.isEmpty(dbjlfs)) {
                                dbjlfs = "0";
                            }
                            String dbrs = dbMap.get(jebzfs);
                            if (StringUtils.isEmpty(dbrs)) {
                                dbrs = "0";
                            }
                            if (new BigDecimal(dbrs).compareTo(new BigDecimal(dbfss)) >= 0) {
                                temp = temp.add(new BigDecimal(dbrs));
                                tcBd = tcBd.add(new BigDecimal(dbjlfs));
                            }
                        }
                    }
                    wczjg = temp.toString();
                } else if (StringUtils.equals(jslj, "7")) {   // 目标奖
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt6ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String fgslbmb = mapTemp.get("fgslbmb");
                        String jebzmb = mapTemp.get("jebzmb");
                        String dbjlmb = mapTemp.get("dbjlmb");
                        if (StringUtils.isNotEmpty(fgslbmb)) {
                            String djTemp = fbDjMap.get(szfb);
                            if (StringUtils.isEmpty(djTemp)) {
                                continue;
                            }
                            if (new BigDecimal(djTemp).compareTo(new BigDecimal(fgslbmb)) != 0) {
                                continue;
                            }
                        }
                        if (StringUtils.isEmpty(jebzmb)) {
                            jebzmb = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(jebzmb)) >= 0) {
                            if (StringUtils.isEmpty(dbjlmb)) {
                                dbjlmb = "0";
                            }
                            tcBd = tcBd.add(new BigDecimal(dbjlmb));
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "8")) {   // 分司补贴
                    bizbmc = "分司类别";
                    List<Map<String, String>> mapList = dt8ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String fslb = mapTemp.get("fslb");
                        String dbjl = mapTemp.get("dbjl");

                        String djTemp = fbDjMap.get(szfb);
                        if (StringUtils.isNotEmpty(fslb)) {
                            if (StringUtils.isEmpty(djTemp)) {
                                continue;
                            }
                            if (new BigDecimal(djTemp).compareTo(new BigDecimal(fslb)) != 0) {
                                continue;
                            }
                        }
                        if (StringUtils.isEmpty(dbjl)) {
                            dbjl = "0";
                        }
                        tcBd = tcBd.add(new BigDecimal(dbjl));
                        if (StringUtils.equals(djTemp, "0")) {
                            wczjg = "A+";
                        } else if (StringUtils.equals(djTemp, "1")) {
                            wczjg = "A";
                        } else if (StringUtils.equals(djTemp, "2")) {
                            wczjg = "B";
                        } else if (StringUtils.equals(djTemp, "3")) {
                            wczjg = "C";
                        } else if (StringUtils.equals(djTemp, "4")) {
                            wczjg = "D";
                        }
                        break;
                    }
                } else if (StringUtils.equals(jslj, "9")) {   // 目标奖_净新签实收
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt9ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String fslb = mapTemp.get("fslb");
                        String wczT = mapTemp.get("wcz");
                        String jjxs = mapTemp.get("jjxs");
                        if (StringUtils.isNotEmpty(fslb)) {
                            String djTemp = fbDjMap.get(szfb);
                            if (StringUtils.isEmpty(djTemp)) {
                                continue;
                            }
                            if (new BigDecimal(djTemp).compareTo(new BigDecimal(fslb)) != 0) {
                                continue;
                            }
                        }
                        if (StringUtils.isEmpty(wczT)) {
                            wczT = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(wczT)) >= 0) {
                            if (StringUtils.isEmpty(jjxs)) {
                                jjxs = "0";
                            }
                            // 获取“净新签实收完成”指标得的值
                            String jxqssWcz = yjMap.get(yg + "_" + jxqsswcIndexCode + "_" + szny);
                            if (StringUtils.isEmpty(jxqssWcz)) {
                                jxqssWcz = "0";
                            }
                            tcBd = new BigDecimal(jxqssWcz).multiply(new BigDecimal(jjxs));
                            if (tcBd.compareTo(new BigDecimal("3000")) < 0 && new BigDecimal(jjxs).compareTo(BigDecimal.ZERO) > 0) {
                                tcBd = new BigDecimal("3000");
                            }
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "10")) {   // 分总销售额提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt10ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    String bms = gsAndBmsMap.get(szfb);
                    if (StringUtils.isEmpty(bms)) {
                        continue;
                    }
                    String bmxsT = "0";
                    for (Map<String, String> mapTemp : mapList) {
                        String khbmsTemp = mapTemp.get("khbms");
                        if (StringUtils.isEmpty(khbmsTemp)) {
                            continue;
                        }
                        String dyxsTemp = mapTemp.get("dyxs");
                        if (new BigDecimal(bms).compareTo(new BigDecimal(khbmsTemp)) == 0) {
                            bmxsT = dyxsTemp;
                            break;
                        }
                    }
                    if (StringUtils.isEmpty(bmxsT)) {
                        bmxsT = "0";
                    }
                    if (StringUtils.isEmpty(fzxsetcbl)) {
                        fzxsetcbl = "0";
                    }
                    tcBd = yjBd.multiply(new BigDecimal(fzxsetcbl)).multiply(new BigDecimal(bmxsT));
                } else if (StringUtils.equals(jslj, "11")) {   // 分总净利润提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt11ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    String bms = gsAndBmsMap.get(szfb);
                    if (StringUtils.isEmpty(bms)) {
                        continue;
                    }
                    String bmxsT = "0";
                    List<Map<String, String>> mapBmxsList = dt10ListMap.get(tcfa);
                    for (Map<String, String> mapTemp : mapBmxsList) {
                        String khbmsTemp = mapTemp.get("khbms");
                        if (StringUtils.isEmpty(khbmsTemp)) {
                            continue;
                        }
                        String dyxsTemp = mapTemp.get("dyxs");
                        if (new BigDecimal(bms).compareTo(new BigDecimal(khbmsTemp)) == 0) {
                            bmxsT = dyxsTemp;
                            break;
                        }
                    }
                    if (StringUtils.isEmpty(bmxsT)) {
                        bmxsT = "0";
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String pjqjks = mapTemp.get("pjqjks");
                        if (StringUtils.isEmpty(pjqjks)) {
                            pjqjks = "0";
                        }
                        String pjqjjsh = mapTemp.get("pjqjjsh");
                        if (StringUtils.isEmpty(pjqjjsh)) {
                            pjqjjsh = "0";
                        }
                        String tcxszl = mapTemp.get("tcxs");
                        if (StringUtils.isEmpty(tcxszl)) {
                            tcxszl = "0";
                        }

                        if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjsh)) <= 0) {
                            BigDecimal multiply = (yjBd.subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        } else if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjsh)) > 0) {
                            BigDecimal multiply = (new BigDecimal(pjqjjsh).subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        }
                    }
                    tcBd = tcBd.multiply(new BigDecimal(bms)).multiply(new BigDecimal(bmxsT));
                } else if (StringUtils.equals(jslj, "12")) {   // 大区总经理/集团副总裁(金融业务)提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        yj = "0";
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    String jlryj = yjMap.get(yg + "_" + lrzbbm + "_" + szny);
                    if (StringUtils.isEmpty(jlryj)) {
                        jlryj = "0";
                    }
                    BigDecimal jlryjBd = new BigDecimal(jlryj);
                    List<Map<String, String>> mapList = dt12ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    String fpj = fpjMap.get(yg + "_" + tcfa + "_" + szny);
                    if (StringUtils.isEmpty(fpj)) {
                        fpj = "0";
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String dyfbbhxjTemp = mapTemp.get("dyfbbhxj");
                        if (StringUtils.isEmpty(dyfbbhxjTemp)) {
                            continue;
                        }
                        Set<String> xjfbSet = fbAndXjListMap.get(dyfbbhxjTemp);
                        if (CollectionUtils.isEmpty(xjfbSet) || !xjfbSet.contains(szfb)) {
                            continue;
                        }
                        String fgqyssblTemp = mapTemp.get("fgqyssbl");
                        if (StringUtils.isEmpty(fgqyssblTemp)) {
                            fgqyssblTemp = "0";
                        }
                        String jlrblTemp = mapTemp.get("jlrbl");
                        if (StringUtils.isEmpty(jlrblTemp)) {
                            jlrblTemp = "0";
                        }
                        String jlrwfssblTemp = mapTemp.get("jlrwfssbl");
                        if (StringUtils.isEmpty(jlrwfssblTemp)) {
                            jlrwfssblTemp = "0";
                        }
                        if (jlryjBd.compareTo(BigDecimal.ZERO) < 0) {
                            BigDecimal fgqyssBd = yjBd.multiply(new BigDecimal(fgqyssblTemp));
                            BigDecimal jlrBd = jlryjBd.multiply(new BigDecimal(jlrwfssblTemp));
                            tcBd = fgqyssBd.add(jlrBd).add(new BigDecimal(fpj));
                        } else {
                            BigDecimal fgqyssBd = yjBd.multiply(new BigDecimal(fgqyssblTemp));
                            BigDecimal jlrBd = jlryjBd.multiply(new BigDecimal(jlrblTemp));
                            tcBd = fgqyssBd.add(jlrBd).add(new BigDecimal(fpj));
                        }
                        break;
                    }
                } else if (StringUtils.equals(jslj, "13")) {   // 区域招商经理提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        yj = "0";
                    }
                    wczjg = yj;
                    BigDecimal yjBdTemp = new BigDecimal(yj);
                    String qyzsjlbm = qyzsjlbmyjMap.get(yg + "_" + tcfa + "_" + szny);
                    String qyzsjlBl = qyzsjlbmyjBlMap.get(yg + "_" + tcfa + "_" + szny);
                    if (StringUtils.isEmpty(qyzsjlBl)) {
                        qyzsjlBl = "0";
                    }
                    BigDecimal yjBd = BigDecimal.ZERO;
                    if (StringUtils.isNotEmpty(qyzsjlbm) && new BigDecimal(qyzsjlbm).compareTo(BigDecimal.ZERO) != 0) {
                        yjBd = yjBdTemp.multiply(new BigDecimal(qyzsjlBl)).divide(new BigDecimal(qyzsjlbm), 6, RoundingMode.HALF_UP);
                    }

                    List<Map<String, String>> mapList = dt13ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String pjqjks = mapTemp.get("pjqjks");
                        if (StringUtils.isEmpty(pjqjks)) {
                            pjqjks = "0";
                        }
                        String pjqjjs = mapTemp.get("pjqjjs");
                        if (StringUtils.isEmpty(pjqjjs)) {
                            pjqjjs = "0";
                        }
                        String tcxszl = mapTemp.get("tcxs");
                        if (StringUtils.isEmpty(tcxszl)) {
                            tcxszl = "0";
                        }

                        if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjs)) <= 0) {
                            BigDecimal multiply = (yjBd.subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        } else if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjs)) > 0) {
                            BigDecimal multiply = (new BigDecimal(pjqjjs).subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        }
                    }
                }

                if (StringUtils.equals(sfqytsgz, "0") && gwSet.contains(yg)) {
                    String ksrq = szny + "-01";
                    String jsrq = szny + "-31";
                    String cnt1 = "0";
                    rs1.execute("select count(*) as cnt\n" +
                            "from kq_format_total a\n" +
                            "Left join hrmresource b on b.id = a.resourceid\n" +
                            "LEFT JOIN KQ_HolidaySet c on a.kqdate = c.holidayDate and a.groupId = c.groupId\n" +
                            "WHERE b.managerid = " + yg + " and (c.changeType = 1 or c.changeType = 2) and a.kqdate >= '"
                            + ksrq + "' and a.kqdate <= '" + jsrq + "'");
                    if (rs1.next()) {
                        cnt1 = rs1.getString("cnt");
                    }
                    if (StringUtils.isNotEmpty(cnt1) && new BigDecimal(cnt1).compareTo(BigDecimal.ZERO) > 0) {
                        rs1.execute("select count(*) as cnt \n" +
                                " from kq_format_total a \n" +
                                " LEFT JOIN KQ_HolidaySet c on a.kqdate = c.holidayDate and a.groupId = c.groupId\n" +
                                " WHERE a.resourceid = " + yg + " and (c.changeType = 1 or c.changeType = 2) and a.kqdate >= '"
                                + ksrq + "' and a.kqdate <= '" + jsrq + "'");
                        if (rs1.next()) {
                            String cnt2 = rs1.getString("cnt");
                            if (StringUtils.isNotEmpty(cnt2) && new BigDecimal(cnt2).compareTo(BigDecimal.ZERO) > 0
                                    && new BigDecimal(cnt1).divide(new BigDecimal(cnt2), 2, RoundingMode.HALF_UP).compareTo(new BigDecimal("8")) > 0) {
                                tcBd = tcBd.multiply(new BigDecimal("0.7"));
                            }
                        }
                    }
                }

                if (StringUtils.isNotEmpty(sfqyeczs) && StringUtils.equals(sfqyeczs, "0")) {
                    String zsxs = zsxsMap.get(yg);
                    if (StringUtils.isNotEmpty(zsxs)) {
                        tcBd = tcBd.multiply(new BigDecimal(zsxs));
                    }
                }

                String key = yg + "_" + tcfa + "_" + szny;
                String oldDataId = oldDataMap.get(key);
                if (StringUtils.isNotEmpty(oldDataId)) {
                    List rowParam = new ArrayList<>();
                    rowParam.add(tcBd.toString());
                    rowParam.add(szfb);
                    rowParam.add(szbm);
                    rowParam.add(yggh);
                    rowParam.add(szgw);
                    rowParam.add(bizbbm);
                    rowParam.add(bizbmc);
                    rowParam.add(wczjg);
                    rowParam.add(dtFormat.format(new Date()));
                    rowParam.add(oldDataId);
                    updateList.add(rowParam);
                } else {
                    List rowParam = new ArrayList<>();
                    rowParam.add(tcfa);
                    rowParam.add(yg);
                    rowParam.add(tcBd.toString());
                    rowParam.add(szny);
                    rowParam.add(szfb);
                    rowParam.add(szbm);
                    rowParam.add(yggh);
                    rowParam.add(szgw);
                    rowParam.add(bizbbm);
                    rowParam.add(bizbmc);
                    rowParam.add(wczjg);
                    rowParam.add(modeId);
                    rowParam.add(cusId);
                    rowParam.add("1");
                    rowParam.add("0");
                    rowParam.add(sdfDate.format(new Date()));
                    rowParam.add(sdfTime.format(new Date()));
                    insertList.add(rowParam);
                }
            }

            if (CollectionUtils.isEmpty(updateList)) {
                baseBean.writeLog("countCommission updateList is null.");
            } else {
                baseBean.writeLog("countCommission updateList size is:" + updateList.size());
                rs.executeBatchSql("UPDATE uf_yjtcjs SET df = ?, szfb = ?, szbm = ?, yggh = ?, szgw = ?, wczcode = ?," +
                        " wczmc = ?, wczjg = ?, modedatamodifydatetime = ? WHERE id = ?", updateList);
            }
            if (CollectionUtils.isEmpty(insertList)) {
                baseBean.writeLog("countCommission insertList is null.");
            } else {
                baseBean.writeLog("countCommission insertList size is:" + insertList.size());
                rs.executeBatchSql("INSERT INTO uf_yjtcjs (tcfa,yg,df,szny,szfb,szbm,yggh,szgw,wczcode,wczmc,wczjg," +
                        "formmodeid,MODEUUID, modedatacreater,modedatacreatertype, modedatacreatedate, modedatacreatetime) " +
                        "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", insertList);
            }
        } catch (Exception e) {
            baseBean.writeLog("countCommission error:" + e);
            result.put("mes", "系统错误，请联系管理员！");
            result.put("code", "400");
            return result;
        }
        baseBean.writeLog("countCommission end.");
        result.put("mes", "正在计算，请稍等！");
        result.put("code", "200");
        return result;
    }

    @Override
    public Map<String, Object> extraCount(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        baseBean.writeLog("extraCount start:" + params);
        try {
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();

            Map<String, List<Map<String, String>>> dt1ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt1");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String pjqjkszl = rs.getString("pjqjkszl");
                String pjqjjszl = rs.getString("pjqjjszl");
                String tcxszl = rs.getString("tcxszl");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("pjqjkszl", pjqjkszl);
                map.put("pjqjjszl", pjqjjszl);
                map.put("tcxszl", tcxszl);
                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt1ListMap)) && dt1ListMap.containsKey(mainid)) {
                    mapList = dt1ListMap.get(mainid);
                }
                mapList.add(map);
                dt1ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt2ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt2");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String pjqjkspt = rs.getString("pjqjkspt");
                String pjqjjspt = rs.getString("pjqjjspt");
                String tcxspt = rs.getString("tcxspt");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("pjqjkspt", pjqjkspt);
                map.put("pjqjjspt", pjqjjspt);
                map.put("tcxspt", tcxspt);
                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt2ListMap)) && dt2ListMap.containsKey(mainid)) {
                    mapList = dt2ListMap.get(mainid);
                }
                mapList.add(map);
                dt2ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt3ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt3");
            while (rs.next()) {
                String dtId = rs.getString("id");
                String mainid = rs.getString("mainid");
                String fgslbjl = rs.getString("fgslbjl");
                String jebzjl = rs.getString("jebzjl");
                String dbrs = rs.getString("dbrs");
                String dbjljl = rs.getString("dbjljl");
                String yxj = rs.getString("yxj");
                if (StringUtils.isEmpty(mainid) || StringUtils.isEmpty(dtId)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("dtId", dtId);
                map.put("fgslbjl", fgslbjl);
                map.put("jebzjl", jebzjl);
                map.put("dbrs", dbrs);
                map.put("dbjljl", dbjljl);
                map.put("yxj", yxj);
                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt3ListMap)) && dt3ListMap.containsKey(mainid)) {
                    mapList = dt3ListMap.get(mainid);
                }
                mapList.add(map);
                dt3ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt4ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt4");
            while (rs.next()) {
                String dtId = rs.getString("id");
                String mainid = rs.getString("mainid");
                String fgslbzj = rs.getString("fgslbzj");
                String jebzzj = rs.getString("jebzzj");
                String dbtds = rs.getString("dbtds");
                String dbjlzj = rs.getString("dbjlzj");
                String yxj = rs.getString("yxj");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("dtId", dtId);
                map.put("fgslbzj", fgslbzj);
                map.put("jebzzj", jebzzj);
                map.put("dbtds", dbtds);
                map.put("dbjlzj", dbjlzj);
                map.put("yxj", yxj);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt4ListMap)) && dt4ListMap.containsKey(mainid)) {
                    mapList = dt4ListMap.get(mainid);
                }
                mapList.add(map);
                dt4ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt5ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt5");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String jebzfs = rs.getString("jebzfs");
                String dbfss = rs.getString("dbfss");
                String dbjlfs = rs.getString("dbjlfs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("jebzfs", jebzfs);
                map.put("dbfss", dbfss);
                map.put("dbjlfs", dbjlfs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt5ListMap)) && dt5ListMap.containsKey(mainid)) {
                    mapList = dt5ListMap.get(mainid);
                }
                mapList.add(map);
                dt5ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt6ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt6");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String fgslbmb = rs.getString("fgslbmb");
                String jebzmb = rs.getString("jebzmb");
                String dbjlmb = rs.getString("dbjlmb");
                String yxj = rs.getString("yxj");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("fgslbmb", fgslbmb);
                map.put("jebzmb", jebzmb);
                map.put("dbjlmb", dbjlmb);
                map.put("yxj", yxj);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt6ListMap)) && dt6ListMap.containsKey(mainid)) {
                    mapList = dt6ListMap.get(mainid);
                }
                mapList.add(map);
                dt6ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt7ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt7");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String mczh = rs.getString("mczh");
                String jezh = rs.getString("jezh");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("mczh", mczh);
                map.put("jezh", jezh);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt7ListMap)) && dt7ListMap.containsKey(mainid)) {
                    mapList = dt7ListMap.get(mainid);
                }
                mapList.add(map);
                dt7ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt8ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt8");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String fslb = rs.getString("fslb");
                String dbjl = rs.getString("dbjl");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("dbjl", dbjl);
                map.put("fslb", fslb);
                map.put("yxj", yxj);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt8ListMap)) && dt8ListMap.containsKey(mainid)) {
                    mapList = dt8ListMap.get(mainid);
                }
                mapList.add(map);
                dt8ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt9ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt9");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String fslb = rs.getString("fslb");
                String wcz = rs.getString("wcz");
                String jjxs = rs.getString("jjxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("wcz", wcz);
                map.put("fslb", fslb);
                map.put("yxj", yxj);
                map.put("jjxs", jjxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt9ListMap)) && dt9ListMap.containsKey(mainid)) {
                    mapList = dt9ListMap.get(mainid);
                }
                mapList.add(map);
                dt9ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt10ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt10");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String khbms = rs.getString("khbms");
                String dyxs = rs.getString("dyxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("khbms", khbms);
                map.put("dyxs", dyxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt10ListMap)) && dt10ListMap.containsKey(mainid)) {
                    mapList = dt10ListMap.get(mainid);
                }
                mapList.add(map);
                dt10ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt11ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt11");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String pjmc = rs.getString("pjmc");
                String pjqjks = rs.getString("pjqjks");
                String pjqjjsh = rs.getString("pjqjjsh");
                String tcxs = rs.getString("tcxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("yxj", yxj);
                map.put("pjmc", pjmc);
                map.put("pjqjks", pjqjks);
                map.put("pjqjjsh", pjqjjsh);
                map.put("tcxs", tcxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt11ListMap)) && dt11ListMap.containsKey(mainid)) {
                    mapList = dt11ListMap.get(mainid);
                }
                mapList.add(map);
                dt11ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt12ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt12");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String dyfbbhxj = rs.getString("dyfbbhxj");
                String fgqyssbl = rs.getString("fgqyssbl");
                String jlrbl = rs.getString("jlrbl");
                String jlrwfssbl = rs.getString("jlrwfssbl");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("yxj", yxj);
                map.put("dyfbbhxj", dyfbbhxj);
                map.put("fgqyssbl", fgqyssbl);
                map.put("jlrbl", jlrbl);
                map.put("jlrwfssbl", jlrwfssbl);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt12ListMap)) && dt12ListMap.containsKey(mainid)) {
                    mapList = dt12ListMap.get(mainid);
                }
                mapList.add(map);
                dt12ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt13ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt13");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String pjqjks = rs.getString("pjqjks");
                String pjqjjs = rs.getString("pjqjjs");
                String tcxs = rs.getString("tcxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("yxj", yxj);
                map.put("pjqjks", pjqjks);
                map.put("pjqjjs", pjqjjs);
                map.put("tcxs", tcxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt13ListMap)) && dt13ListMap.containsKey(mainid)) {
                    mapList = dt13ListMap.get(mainid);
                }
                mapList.add(map);
                dt13ListMap.put(mainid, mapList);
            }

            // 获取分部对应的所有上级分部
            Map<String, String> fbAndSupMap = new HashMap<>();
            rs.execute("SELECT id, supsubcomid FROM hrmsubcompany WHERE canceled != 1 or canceled is null");
            while (rs.next()) {
                String fbId = rs.getString("id");
                if (StringUtils.isEmpty(fbId)) {
                    continue;
                }
                String supsubcomid = rs.getString("supsubcomid");
                fbAndSupMap.put(fbId, supsubcomid);
            }
            Map<String, Set<String>> fbAndXjListMap = new HashMap<>();
            for (Map.Entry<String, String> entry : fbAndSupMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (StringUtils.isNotEmpty(key)) {
                    Set<String> tempSet = new HashSet<>();
                    if ((!CollectionUtils.isEmpty(fbAndXjListMap)) && fbAndXjListMap.containsKey(key)) {
                        tempSet = fbAndXjListMap.get(key);
                    }
                    tempSet.add(key);
                    fbAndXjListMap.put(key, tempSet);
                }
                if (StringUtils.isNotEmpty(value)) {
                    Set<String> tempSet = new HashSet<>();
                    if ((!CollectionUtils.isEmpty(fbAndXjListMap)) && fbAndXjListMap.containsKey(value)) {
                        tempSet = fbAndXjListMap.get(value);
                    }
                    tempSet.add(value);
                    tempSet.add(key);
                    fbAndXjListMap.put(value, tempSet);
                }
            }

            // 获取公司对应部门数
            Map<String, Map<String, String>> gsAndBmsMapMap = new HashMap<>();
            rs.execute("SELECT szny, dyfs, khbms FROM uf_khbms");
            while (rs1.next()) {
                String dyfsTemp = rs.getString("dyfs");
                String sznyTemp = rs.getString("szny");
                if (StringUtils.isEmpty(dyfsTemp) || StringUtils.isEmpty(sznyTemp)) {
                    continue;
                }
                String khbmsTemp = rs.getString("khbms");
                Map<String, String> gsAndBmsMap = new HashMap<>();
                if ((!CollectionUtils.isEmpty(gsAndBmsMapMap)) && gsAndBmsMapMap.containsKey(sznyTemp)) {
                    gsAndBmsMap = gsAndBmsMapMap.get(sznyTemp);
                }
                gsAndBmsMap.put(dyfsTemp, khbmsTemp);
                gsAndBmsMapMap.put(sznyTemp, gsAndBmsMap);
            }

            // 获取扶贫金
            Map<String, String> fpjMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_fpj ");
            while (rs.next()) {
                String yg = rs.getString("yg");
                String sztcfa = rs.getString("sztcfa");
                String sznyFpj = rs.getString("szny");
                String jeFpj = rs.getString("je");
                if (StringUtils.isEmpty(yg) || StringUtils.isEmpty(sztcfa) || StringUtils.isEmpty(sznyFpj)) {
                    continue;
                }
                fpjMap.put(yg + "_" + sztcfa + "_" + sznyFpj, jeFpj);
            }

            // 区域招商经理服务部门数及业绩比例
            Map<String, String> qyzsjlbmyjMap = new HashMap<>();
            Map<String, String> qyzsjlbmyjBlMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_qyzsjlfwbmsyjbl ");
            while (rs.next()) {
                String yg = rs.getString("yg");
                String sztcfa = rs.getString("szkhfa");
                String sznyBmyj = rs.getString("szny");
                String fwbms = rs.getString("fwbms");
                String fsthssbl = rs.getString("fsthssbl");
                if (StringUtils.isEmpty(yg) || StringUtils.isEmpty(sztcfa) || StringUtils.isEmpty(sznyBmyj)) {
                    continue;
                }
                qyzsjlbmyjMap.put(yg + "_" + sztcfa + "_" + sznyBmyj, fwbms);
                qyzsjlbmyjBlMap.put(yg + "_" + sztcfa + "_" + sznyBmyj, fsthssbl);
            }

            // “净新签实收完成”指标编码
            String jxqsswcIndexCode = rs.getPropValue("qz_tc_zm_prop", "jxqsswc_index_code");
            // 获取岗位名称为“销售经理”的人员id
            // 岗位fieldId 3
            String gwFieldId = rs.getPropValue("qz_tc_zm_prop", "gw_field_id_tc");
            Set<String> gwSet = new HashSet<>();
            rs.execute("SELECT b.id,b." + gwFieldId + ",c.gwmc FROM cus_fielddata b LEFT JOIN uf_JCL_GWGL c " +
                    " ON b." + gwFieldId + " = c.id WHERE b.scope = 'HrmCustomFieldByInfoType' AND b.scopeid = 3 AND b." + gwFieldId
                    + " is not null AND c.gwmc = '销售经理'");
            while (rs.next()) {
                String id = rs.getString("id");
                String gwmc = rs.getString("gwmc");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(gwmc) || !StringUtils.equals(gwmc, "销售经理")) {
                    continue;
                }
                gwSet.add(id);
            }

            String today = DateUtil.getCurrentDate();

            // 获取当前生效的分司以及对应级别
            Map<String, String> fbDjMap = new HashMap<>();
            rs.execute("SELECT fb, lb\n" +
                    "FROM uf_yjtl\n" +
                    "WHERE shenxrq <= '" + today + "' AND sxrq >= '" + today + "'\n" +
                    "ORDER BY CONVERT(DATE, shenxrq, 120) DESC");
            while (rs.next()) {
                String fb = rs.getString("fb");
                String lb = rs.getString("lb");
                if (StringUtils.isEmpty(fb) || StringUtils.isEmpty(lb)) {
                    continue;
                }
                fbDjMap.put(fb, lb);
            }

            // 获取BI系统中间表结果映射
            Map<String, String> yjMap = new HashMap<>();
            Map<String, String> fsYjMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_ewsjjs_dt2 ");
            while (rs.next()) {
                String yg = rs.getString("yg");
                String gh = rs.getString("gh");
                String szzq = rs.getString("szzq");
                String bizbxm = rs.getString("bizbxm");
                if (StringUtils.isEmpty(szzq) || StringUtils.isEmpty(bizbxm)) {
                    continue;
                }
                String wcz = rs.getString("wcz");
                if (StringUtils.isNotEmpty(yg)) {
                    yjMap.put(yg + "_" + bizbxm + "_" + szzq, wcz);
                }
                if (StringUtils.isNotEmpty(gh) && StringUtils.isEmpty(yg)) {
                    fsYjMap.put(gh + "_" + bizbxm + "_" + szzq, wcz);
                }
            }

            // 获取二次折算配置
            Map<String, String> zsxsMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_TCECZSPZ ");
            while (rs.next()) {
                String xm = rs.getString("xm");
                if (StringUtils.isEmpty(xm)) {
                    continue;
                }
                String fadjxs = rs.getString("fadjxs");
                String zjdjxs = rs.getString("zjdjxs");
                String sjdjtds = rs.getString("sjdjtds");
                if (StringUtils.isEmpty(sjdjtds)) {
                    sjdjtds = "0";
                }
                String zsxs = null;
                if (StringUtils.isEmpty(fadjxs) && StringUtils.isEmpty(zjdjxs)) {
                    if (new BigDecimal(sjdjtds).compareTo(new BigDecimal("2")) > 0) {
                        zsxs = "0.85";
                    } else {
                        zsxs = "1";
                    }
                } else {
                    if (StringUtils.isNotEmpty(zjdjxs)) {
                        zsxs = zjdjxs;
                    }
                    if (StringUtils.isNotEmpty(fadjxs)) {
                        zsxs = fadjxs;
                    }
                }
                zsxsMap.put(xm, zsxs);
            }

            List<List> updateList = new ArrayList<>();

            // 定义一个Comparator来根据Map中key为"pjqjkszl"的value进行排序
            Comparator<Map<String, String>> comparator = Comparator.comparingInt(
                    map -> Integer.parseInt(map.getOrDefault("pjqjkszl", "0")) // 使用getOrDefault来避免NullPointerException
            );

            Comparator<Map<String, String>> comparator1 = Comparator.comparingInt(
                    map -> Integer.parseInt(map.getOrDefault("jebzfs", "0")) // 使用getOrDefault来避免NullPointerException
            );

            Comparator<Map<String, String>> comparator2 = Comparator.comparingInt(
                    map -> Integer.parseInt(map.getOrDefault("yxj", "0")) // 使用getOrDefault来避免NullPointerException
            );

            String sql = "SELECT a.id, a.tcfa, a.yg, a.szny, b.fzxsetcbl, b.jslj, b.dqzjljtfzcjrywdqzjljtfzcjrywjl, b.sjlb," +
                    " b.sfqyeczs, b.sfqytsgz, c.bizbbm, d.subcompanyid1, c1.bizbbm as lrzbbm, c1.bizbmc as lrzbmc " +
                    " FROM uf_ewsjjs a " +
                    " LEFT JOIN uf_tcfasz b ON a.tcfa = b.id " +
                    " LEFT JOIN uf_bizbys c ON b.sjlb = c.id " +
                    " LEFT JOIN uf_bizbys c1 ON b.dqzjljtfzcjrywdqzjljtfzcjrywjl = c1.id " +
                    " LEFT JOIN hrmresource d ON d.id = a.yg " +
                    " WHERE 1 = 1 ";
            String ryId = Util.null2String(params.get("ryid"));
            if (StringUtils.isNotEmpty(ryId)) {
                sql = sql + " AND a.yg in (" + ryId + ") ";
            }
            rs.execute(sql);
            while (rs.next()) {
                String idOld = rs.getString("id");
                String tcfa = rs.getString("tcfa");
                String yg = rs.getString("yg");
                String szny = rs.getString("szny");
                String jslj = rs.getString("jslj");
                String sjlb = rs.getString("sjlb");
                String bizbbm = rs.getString("bizbbm");
                String szfb = rs.getString("subcompanyid1");
                String sfqytsgz = rs.getString("sfqytsgz");
                String sfqyeczs = rs.getString("sfqyeczs");

                String fzxsetcbl = rs.getString("fzxsetcbl");
                String lrzbId = rs.getString("dqzjljtfzcjrywdqzjljtfzcjrywjl");
                String lrzbbm = rs.getString("lrzbbm");
                String lrzbmc = rs.getString("lrzbmc");

                if (StringUtils.isEmpty(tcfa) || StringUtils.isEmpty(yg) || StringUtils.isEmpty(szny) || StringUtils.isEmpty(jslj)
                        || StringUtils.isEmpty(sjlb)) {
                    continue;
                }
                Map<String, String> gsAndBmsMap = gsAndBmsMapMap.get(szny);

                BigDecimal tcBd = new BigDecimal("0");

                String wczjg = null;
                // 增量牌级
                if (StringUtils.equals(jslj, "0")) {
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt1ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String pjqjkszl = mapTemp.get("pjqjkszl");
                        if (StringUtils.isEmpty(pjqjkszl)) {
                            pjqjkszl = "0";
                        }
                        String pjqjjszl = mapTemp.get("pjqjjszl");
                        if (StringUtils.isEmpty(pjqjjszl)) {
                            pjqjjszl = "0";
                        }
                        String tcxszl = mapTemp.get("tcxszl");
                        if (StringUtils.isEmpty(tcxszl)) {
                            tcxszl = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(pjqjkszl)) >= 0 && yjBd.compareTo(new BigDecimal(pjqjjszl)) < 0) {
                            BigDecimal multiply = (yjBd.subtract(new BigDecimal(pjqjkszl))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        } else if (yjBd.compareTo(new BigDecimal(pjqjkszl)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjszl)) >= 0) {
                            BigDecimal multiply = (new BigDecimal(pjqjjszl).subtract(new BigDecimal(pjqjkszl))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        }
                    }
                } else if (StringUtils.equals(jslj, "1")) {   // 普通牌级
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt2ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    for (Map<String, String> mapTemp : mapList) {
                        String pjqjkspt = mapTemp.get("pjqjkspt");
                        if (StringUtils.isEmpty(pjqjkspt)) {
                            pjqjkspt = "0";
                        }
                        String pjqjjspt = mapTemp.get("pjqjjspt");
                        if (StringUtils.isEmpty(pjqjjspt)) {
                            pjqjjspt = "0";
                        }
                        String tcxspt = mapTemp.get("tcxspt");
                        if (StringUtils.isEmpty(tcxspt)) {
                            tcxspt = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(pjqjkspt)) >= 0 && yjBd.compareTo(new BigDecimal(pjqjjspt)) < 0) {
                            tcBd = yjBd.multiply(new BigDecimal(tcxspt));
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "2") || StringUtils.equals(jslj, "3")) {   // 顾问综合排名奖、经理/总监综合排名奖
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt7ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    for (Map<String, String> mapTemp : mapList) {
                        String mczh = mapTemp.get("mczh");
                        if (StringUtils.isEmpty(mczh)) {
                            continue;
                        }
                        String jezh = mapTemp.get("jezh");
                        if (StringUtils.isEmpty(jezh)) {
                            jezh = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(mczh)) == 0) {
                            tcBd = new BigDecimal(jezh);
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "4")) {   // 经理合格奖、总监/分总合格奖
                    List<Map<String, String>> mapList = dt3ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    Map<String, String> jlMap = new HashMap<>();
                    Map<String, String> rsbzMap = new HashMap<>();
                    Map<String, String> dbCntMap = new HashMap<>();
                    rs1.execute("select id, subcompanyid1 from hrmresource where managerid =" + yg);
                    while (rs1.next()) {
                        String id = rs1.getString("id");
                        String subcompanyid1 = rs1.getString("subcompanyid1");
                        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(subcompanyid1)) {
                            continue;
                        }
                        String yj = yjMap.get(id + "_" + bizbbm + "_" + szny);
                        if (StringUtils.isEmpty(yj)) {
                            continue;
                        }
                        for (Map<String, String> mapTemp : mapListNew) {
                            String dtId = mapTemp.get("dtId");
                            if (StringUtils.isEmpty(dtId)) {
                                continue;
                            }
                            String fgslbjl = mapTemp.get("fgslbjl");
                            if (StringUtils.isNotEmpty(fgslbjl)) {
                                String djTemp = fbDjMap.get(subcompanyid1);
                                if (StringUtils.isEmpty(djTemp)) {
                                    continue;
                                }
                                if (new BigDecimal(djTemp).compareTo(new BigDecimal(fgslbjl)) != 0) {
                                    continue;
                                }
                            }
                            String jebzjl = mapTemp.get("jebzjl");
                            if (StringUtils.isEmpty(jebzjl)) {
                                jebzjl = "0";
                            }
                            if (new BigDecimal(yj).compareTo(new BigDecimal(jebzjl)) >= 0) {
                                String dbrs = mapTemp.get("dbrs");
                                String dbjljl = mapTemp.get("dbjljl");

                                rsbzMap.put(dtId, dbrs);
                                jlMap.put(dtId, dbjljl);

                                String cntStr = dbCntMap.get(dtId);
                                if (StringUtils.isEmpty(cntStr)) {
                                    cntStr = "0";
                                }
                                dbCntMap.put(dtId, new BigDecimal(cntStr).add(new BigDecimal("1")).toString());
                                break;
                            }
                        }
                    }

                    BigDecimal temp = new BigDecimal("0");
                    for (Map.Entry<String, String> mapTemp : dbCntMap.entrySet()) {
                        String key = mapTemp.getKey();
                        String value = mapTemp.getValue();
                        if (StringUtils.isEmpty(value)) {
                            continue;
                        }
                        BigDecimal valueBd = new BigDecimal(value);
                        if (valueBd.compareTo(BigDecimal.ZERO) == 0) {
                            continue;
                        }
                        String rsbz = rsbzMap.get(key);
                        if (StringUtils.isEmpty(rsbz)) {
                            rsbz = "0";
                        }
                        if (valueBd.compareTo(new BigDecimal(rsbz)) >= 0) {
                            temp = temp.add(valueBd);
                            String jl = jlMap.get(key);
                            if (StringUtils.isEmpty(jl)) {
                                jl = "0";
                            }
                            tcBd = tcBd.add(new BigDecimal(jl));
                        }
                    }
                    wczjg = temp.toString();
                } else if (StringUtils.equals(jslj, "5")) {   // 总监/分总合格奖
                    List<Map<String, String>> mapList = dt4ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    Map<String, String> rsbzMap = new HashMap<>();
                    Map<String, String> jlMap = new HashMap<>();
                    Map<String, String> dbCntMap = new HashMap<>();
                    rs1.execute("select id, subcompanyid1 from hrmresource where managerid =" + yg);
                    while (rs1.next()) {
                        String id = rs1.getString("id");
                        String subcompanyid1 = rs1.getString("subcompanyid1");
                        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(subcompanyid1)) {
                            continue;
                        }
                        String yj = yjMap.get(id + "_" + bizbbm + "_" + szny);
                        if (StringUtils.isEmpty(yj)) {
                            continue;
                        }
                        for (Map<String, String> mapTemp : mapListNew) {
                            String dtId = mapTemp.get("dtId");
                            if (StringUtils.isEmpty(dtId)) {
                                continue;
                            }
                            String fgslbjl = mapTemp.get("fgslbzj");
                            if (StringUtils.isNotEmpty(fgslbjl)) {
                                String djTemp = fbDjMap.get(subcompanyid1);
                                if (StringUtils.isEmpty(djTemp)) {
                                    continue;
                                }
                                if (new BigDecimal(djTemp).compareTo(new BigDecimal(fgslbjl)) != 0) {
                                    continue;
                                }
                            }
                            String jebzjl = mapTemp.get("jebzzj");
                            if (StringUtils.isEmpty(jebzjl)) {
                                jebzjl = "0";
                            }
                            if (new BigDecimal(yj).compareTo(new BigDecimal(jebzjl)) >= 0) {
                                String dbrs = mapTemp.get("dbtds");
                                String dbjljl = mapTemp.get("dbjlzj");

                                rsbzMap.put(dtId, dbrs);
                                jlMap.put(dtId, dbjljl);

                                String cntStr = dbCntMap.get(dtId);
                                if (StringUtils.isEmpty(cntStr)) {
                                    cntStr = "0";
                                }
                                dbCntMap.put(dtId, new BigDecimal(cntStr).add(new BigDecimal("1")).toString());
                                break;
                            }
                        }
                    }

                    BigDecimal temp = new BigDecimal("0");
                    for (Map.Entry<String, String> mapTemp : dbCntMap.entrySet()) {
                        String key = mapTemp.getKey();
                        String value = mapTemp.getValue();
                        if (StringUtils.isEmpty(value)) {
                            continue;
                        }
                        BigDecimal valueBd = new BigDecimal(value);
                        if (valueBd.compareTo(BigDecimal.ZERO) == 0) {
                            continue;
                        }
                        String rsbz = rsbzMap.get(key);
                        if (StringUtils.isEmpty(rsbz)) {
                            rsbz = "0";
                        }
                        if (valueBd.compareTo(new BigDecimal(rsbz)) >= 0) {
                            temp = temp.add(valueBd);
                            String jl = jlMap.get(key);
                            if (StringUtils.isEmpty(jl)) {
                                jl = "0";
                            }
                            tcBd = tcBd.add(new BigDecimal(jl));
                        }
                    }

                    wczjg = temp.toString();
                } else if (StringUtils.equals(jslj, "6")) {   // 盈利分司奖
                    List<Map<String, String>> mapList = dt5ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator1)
                            .collect(Collectors.toList());
                    Map<String, String> dbMap = new HashMap<>();
                    rs1.execute("select a.id,b.bifsbm from \n" +
                            "(select id from hrmsubcompany where (canceled != 1 or canceled is null) and supsubcomid = (select subcompanyid1\n" +
                            "from hrmresource where id = " + yg + ")\n" +
                            "union \n" +
                            "select subcompanyid1 as id\n" +
                            "from hrmresource where id = " + yg + ") a\n" +
                            "left join uf_BIfsyszjb b on a.id = b.dyfb");
                    while (rs1.next()) {
                        String bifsbm = rs1.getString("bifsbm");
                        if (StringUtils.isEmpty(bifsbm)) {
                            continue;
                        }
                        String yj = yjMap.get(bifsbm + "_" + bizbbm + "_" + szny);
                        if (StringUtils.isEmpty(yj)) {
                            continue;
                        }
                        for (int i = mapListNew.size(); i >= 0; i--) {
                            String jebzfs = mapListNew.get(i).get("jebzfs");
                            if (StringUtils.isEmpty(jebzfs)) {
                                continue;
                            }
                            if (new BigDecimal(yj).compareTo(new BigDecimal(jebzfs)) > 0) {
                                String jl = dbMap.get(jebzfs);
                                if (StringUtils.isEmpty(jl)) {
                                    jl = "0";
                                }
                                dbMap.put(jebzfs, new BigDecimal(jl).add(new BigDecimal("1")).toString());
                                break;
                            }
                        }
                    }
                    if (!CollectionUtils.isEmpty(dbMap)) {
                        for (Map<String, String> mapTemp : mapListNew) {
                            String jebzfs = mapTemp.get("jebzfs");
                            if (StringUtils.isEmpty(jebzfs)) {
                                continue;
                            }
                            String dbfss = mapTemp.get("dbfss");
                            if (StringUtils.isEmpty(dbfss)) {
                                continue;
                            }
                            String dbjlfs = mapTemp.get("dbjlfs");
                            if (StringUtils.isEmpty(dbjlfs)) {
                                dbjlfs = "0";
                            }
                            String dbrs = dbMap.get(jebzfs);
                            if (StringUtils.isEmpty(dbrs)) {
                                dbrs = "0";
                            }
                            if (new BigDecimal(dbrs).compareTo(new BigDecimal(dbfss)) > 0) {
                                tcBd = tcBd.add(new BigDecimal(dbjlfs));
                            }
                        }
                    }
                } else if (StringUtils.equals(jslj, "7")) {   // 目标奖
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt6ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String fgslbmb = mapTemp.get("fgslbmb");
                        String jebzmb = mapTemp.get("jebzmb");
                        String dbjlmb = mapTemp.get("dbjlmb");
                        if (StringUtils.isNotEmpty(fgslbmb)) {
                            String djTemp = fbDjMap.get(szfb);
                            if (StringUtils.isEmpty(djTemp)) {
                                continue;
                            }
                            if (new BigDecimal(djTemp).compareTo(new BigDecimal(fgslbmb)) != 0) {
                                continue;
                            }
                        }
                        if (StringUtils.isEmpty(jebzmb)) {
                            jebzmb = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(jebzmb)) >= 0) {
                            if (StringUtils.isEmpty(dbjlmb)) {
                                dbjlmb = "0";
                            }
                            tcBd = tcBd.add(new BigDecimal(dbjlmb));
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "8")) {   // 分司补贴
//                    bizbmc = "分司类别";
                    List<Map<String, String>> mapList = dt8ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String fslb = mapTemp.get("fslb");
                        String dbjl = mapTemp.get("dbjl");

                        String djTemp = fbDjMap.get(szfb);
                        if (StringUtils.isNotEmpty(fslb)) {
                            if (StringUtils.isEmpty(djTemp)) {
                                continue;
                            }
                            if (new BigDecimal(djTemp).compareTo(new BigDecimal(fslb)) != 0) {
                                continue;
                            }
                        }
                        if (StringUtils.isEmpty(dbjl)) {
                            dbjl = "0";
                        }
                        tcBd = tcBd.add(new BigDecimal(dbjl));
                        if (StringUtils.equals(djTemp, "0")) {
                            wczjg = "A+";
                        } else if (StringUtils.equals(djTemp, "1")) {
                            wczjg = "A";
                        } else if (StringUtils.equals(djTemp, "2")) {
                            wczjg = "B";
                        } else if (StringUtils.equals(djTemp, "3")) {
                            wczjg = "C";
                        } else if (StringUtils.equals(djTemp, "4")) {
                            wczjg = "D";
                        }
                        break;
                    }
                } else if (StringUtils.equals(jslj, "9")) {   // 目标奖_净新签实收
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt9ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String fslb = mapTemp.get("fslb");
                        String wczT = mapTemp.get("wcz");
                        String jjxs = mapTemp.get("jjxs");
                        if (StringUtils.isNotEmpty(fslb)) {
                            String djTemp = fbDjMap.get(szfb);
                            if (StringUtils.isEmpty(djTemp)) {
                                continue;
                            }
                            if (new BigDecimal(djTemp).compareTo(new BigDecimal(fslb)) != 0) {
                                continue;
                            }
                        }
                        if (StringUtils.isEmpty(wczT)) {
                            wczT = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(wczT)) >= 0) {
                            if (StringUtils.isEmpty(jjxs)) {
                                jjxs = "0";
                            }
                            // 获取“净新签实收完成”指标得的值
                            String jxqssWcz = yjMap.get(yg + "_" + jxqsswcIndexCode + "_" + szny);
                            if (StringUtils.isEmpty(jxqssWcz)) {
                                jxqssWcz = "0";
                            }
                            tcBd = new BigDecimal(jxqssWcz).multiply(new BigDecimal(jjxs));
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "10")) {   // 分总销售额提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt10ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    String bms = gsAndBmsMap.get(szfb);
                    if (StringUtils.isEmpty(bms)) {
                        continue;
                    }
                    String bmxsT = "0";
                    for (Map<String, String> mapTemp : mapList) {
                        String khbmsTemp = mapTemp.get("khbms");
                        if (StringUtils.isEmpty(khbmsTemp)) {
                            continue;
                        }
                        String dyxsTemp = mapTemp.get("dyxs");
                        if (new BigDecimal(bms).compareTo(new BigDecimal(khbmsTemp)) == 0) {
                            bmxsT = dyxsTemp;
                            break;
                        }
                    }
                    if (StringUtils.isEmpty(bmxsT)) {
                        bmxsT = "0";
                    }
                    if (StringUtils.isEmpty(fzxsetcbl)) {
                        fzxsetcbl = "0";
                    }
                    tcBd = yjBd.multiply(new BigDecimal(fzxsetcbl)).multiply(new BigDecimal(bmxsT));
                } else if (StringUtils.equals(jslj, "11")) {   // 分总净利润提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt11ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    String bms = gsAndBmsMap.get(szfb);
                    if (StringUtils.isEmpty(bms)) {
                        continue;
                    }
                    String bmxsT = "0";
                    List<Map<String, String>> mapBmxsList = dt10ListMap.get(tcfa);
                    for (Map<String, String> mapTemp : mapBmxsList) {
                        String khbmsTemp = mapTemp.get("khbms");
                        if (StringUtils.isEmpty(khbmsTemp)) {
                            continue;
                        }
                        String dyxsTemp = mapTemp.get("dyxs");
                        if (new BigDecimal(bms).compareTo(new BigDecimal(khbmsTemp)) == 0) {
                            bmxsT = dyxsTemp;
                            break;
                        }
                    }
                    if (StringUtils.isEmpty(bmxsT)) {
                        bmxsT = "0";
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String pjqjks = mapTemp.get("pjqjks");
                        if (StringUtils.isEmpty(pjqjks)) {
                            pjqjks = "0";
                        }
                        String pjqjjsh = mapTemp.get("pjqjjsh");
                        if (StringUtils.isEmpty(pjqjjsh)) {
                            pjqjjsh = "0";
                        }
                        String tcxszl = mapTemp.get("tcxs");
                        if (StringUtils.isEmpty(tcxszl)) {
                            tcxszl = "0";
                        }

                        if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjsh)) <= 0) {
                            BigDecimal multiply = (yjBd.subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        } else if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjsh)) > 0) {
                            BigDecimal multiply = (new BigDecimal(pjqjjsh).subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        }
                    }
                    tcBd = tcBd.multiply(new BigDecimal(bms)).multiply(new BigDecimal(bmxsT));
                } else if (StringUtils.equals(jslj, "12")) {   // 大区总经理/集团副总裁(金融业务)提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        yj = "0";
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    String jlryj = yjMap.get(yg + "_" + lrzbbm + "_" + szny);
                    if (StringUtils.isEmpty(jlryj)) {
                        jlryj = "0";
                    }
                    BigDecimal jlryjBd = new BigDecimal(jlryj);
                    List<Map<String, String>> mapList = dt12ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    String fpj = fpjMap.get(yg + "_" + tcfa + "_" + szny);
                    if (StringUtils.isEmpty(fpj)) {
                        fpj = "0";
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String dyfbbhxjTemp = mapTemp.get("dyfbbhxj");
                        if (StringUtils.isEmpty(dyfbbhxjTemp)) {
                            continue;
                        }
                        Set<String> xjfbSet = fbAndXjListMap.get(dyfbbhxjTemp);
                        if (CollectionUtils.isEmpty(xjfbSet) || !xjfbSet.contains(szfb)) {
                            continue;
                        }
                        String fgqyssblTemp = mapTemp.get("fgqyssbl");
                        if (StringUtils.isEmpty(fgqyssblTemp)) {
                            fgqyssblTemp = "0";
                        }
                        String jlrblTemp = mapTemp.get("jlrbl");
                        if (StringUtils.isEmpty(jlrblTemp)) {
                            jlrblTemp = "0";
                        }
                        String jlrwfssblTemp = mapTemp.get("jlrwfssbl");
                        if (StringUtils.isEmpty(jlrwfssblTemp)) {
                            jlrwfssblTemp = "0";
                        }
                        if (jlryjBd.compareTo(BigDecimal.ZERO) < 0) {
                            BigDecimal fgqyssBd = yjBd.multiply(new BigDecimal(fgqyssblTemp));
                            BigDecimal jlrBd = jlryjBd.multiply(new BigDecimal(jlrwfssblTemp));
                            tcBd = fgqyssBd.add(jlrBd).add(new BigDecimal(fpj));
                        } else {
                            BigDecimal fgqyssBd = yjBd.multiply(new BigDecimal(fgqyssblTemp));
                            BigDecimal jlrBd = jlryjBd.multiply(new BigDecimal(jlrblTemp));
                            tcBd = fgqyssBd.add(jlrBd).add(new BigDecimal(fpj));
                        }
                        break;
                    }
                }else if (StringUtils.equals(jslj, "13")) {   // 区域招商经理提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        yj = "0";
                    }
                    wczjg = yj;
                    BigDecimal yjBdTemp = new BigDecimal(yj);
                    String qyzsjlbm = qyzsjlbmyjMap.get(yg + "_" + tcfa + "_" + szny);
                    String qyzsjlBl = qyzsjlbmyjBlMap.get(yg + "_" + tcfa + "_" + szny);
                    if (StringUtils.isEmpty(qyzsjlBl)) {
                        qyzsjlBl = "0";
                    }
                    BigDecimal yjBd = BigDecimal.ZERO;
                    if (StringUtils.isNotEmpty(qyzsjlbm) && new BigDecimal(qyzsjlbm).compareTo(BigDecimal.ZERO) != 0) {
                        yjBd = yjBdTemp.multiply(new BigDecimal(qyzsjlBl)).divide(new BigDecimal(qyzsjlbm), 6, RoundingMode.HALF_UP);
                    }

                    List<Map<String, String>> mapList = dt13ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String pjqjks = mapTemp.get("pjqjks");
                        if (StringUtils.isEmpty(pjqjks)) {
                            pjqjks = "0";
                        }
                        String pjqjjs = mapTemp.get("pjqjjs");
                        if (StringUtils.isEmpty(pjqjjs)) {
                            pjqjjs = "0";
                        }
                        String tcxszl = mapTemp.get("tcxs");
                        if (StringUtils.isEmpty(tcxszl)) {
                            tcxszl = "0";
                        }

                        if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjs)) <= 0) {
                            BigDecimal multiply = (yjBd.subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        } else if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjs)) > 0) {
                            BigDecimal multiply = (new BigDecimal(pjqjjs).subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        }
                    }
                }

                if (StringUtils.equals(sfqytsgz, "0") && gwSet.contains(yg)) {
                    String ksrq = szny + "-01";
                    String jsrq = szny + "-31";
                    String cnt1 = "0";
                    rs1.execute("select count(*) as cnt\n" +
                            "from kq_format_total a\n" +
                            "Left join hrmresource b on b.id = a.resourceid\n" +
                            "LEFT JOIN KQ_HolidaySet c on a.kqdate = c.holidayDate and a.groupId = c.groupId\n" +
                            "WHERE b.managerid = " + yg + " and (c.changeType = 1 or c.changeType = 2) and a.kqdate >= '"
                            + ksrq + "' and a.kqdate <= '" + jsrq + "'");
                    if (rs1.next()) {
                        cnt1 = rs1.getString("cnt");
                    }
                    if (StringUtils.isNotEmpty(cnt1) && new BigDecimal(cnt1).compareTo(BigDecimal.ZERO) > 0) {
                        rs1.execute("select count(*) as cnt \n" +
                                " from kq_format_total a \n" +
                                " LEFT JOIN KQ_HolidaySet c on a.kqdate = c.holidayDate and a.groupId = c.groupId\n" +
                                " WHERE a.resourceid = " + yg + " and (c.changeType = 1 or c.changeType = 2) and a.kqdate >= '"
                                + ksrq + "' and a.kqdate <= '" + jsrq + "'");
                        if (rs1.next()) {
                            String cnt2 = rs1.getString("cnt");
                            if (StringUtils.isNotEmpty(cnt2) && new BigDecimal(cnt2).compareTo(BigDecimal.ZERO) > 0
                                    && new BigDecimal(cnt1).divide(new BigDecimal(cnt2), 2, RoundingMode.HALF_UP).compareTo(new BigDecimal("8")) > 0) {
                                tcBd = tcBd.multiply(new BigDecimal("0.7"));
                            }
                        }
                    }
                }

                if (StringUtils.isNotEmpty(sfqyeczs) && StringUtils.equals(sfqyeczs, "0")) {
                    String zsxs = zsxsMap.get(yg);
                    if (StringUtils.isNotEmpty(zsxs)) {
                        tcBd = tcBd.multiply(new BigDecimal(zsxs));
                    }
                }

                List rowParam = new ArrayList<>();
                rowParam.add(tcBd.toString());
                rowParam.add(idOld);
                updateList.add(rowParam);
            }

            if (CollectionUtils.isEmpty(updateList)) {
                baseBean.writeLog("extraCount updateList is null.");
            } else {
                baseBean.writeLog("extraCount updateList size is:" + updateList.size());
                rs.executeBatchSql("UPDATE uf_ewsjjs SET df = ? WHERE id = ?", updateList);
            }
        } catch (Exception e) {
            baseBean.writeLog("extraCount error:" + e);
            result.put("mes", "系统错误，请联系管理员！");
            result.put("code", "400");
            return result;
        }
        baseBean.writeLog("extraCount end.");
        result.put("mes", "正在计算，请稍等！");
        result.put("code", "200");
        return result;
    }

    @Override
    public Map<String, Object> countAgainCommission(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        baseBean.writeLog("countAgainCommission start:" + params);
        try {
            String ny = Util.null2String(params.get("ny"));
            baseBean.writeLog("countAgainCommission ny:" + ny);
            if (StringUtils.isEmpty(ny)) {
                baseBean.writeLog("countAgainCommission ny is null.");
                result.put("mes", "年月不可为空！");
                result.put("code", "400");
                return result;
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            YearMonth yearMonth = YearMonth.parse(ny, formatter);
            YearMonth previousYearMonth = yearMonth.minusMonths(1);
            String previousYearMonthStr = previousYearMonth.format(formatter);
            RecordSet rs = new RecordSet();
            Set<String> faIdSet = new HashSet<>();
            rs.execute("SELECT id, sxny, shixny FROM uf_tcfasz WHERE sfxyjehs = 0 AND sxny is not null and shixny is not null");
            while (rs.next()) {
                String id = rs.getString("id");
                String sxny = rs.getString("sxny");
                String shixny = rs.getString("shixny");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(sxny) || StringUtils.isEmpty(shixny)) {
                    continue;
                }
                YearMonth sxnyYM = YearMonth.parse(sxny, formatter);
                YearMonth shixnyYM = YearMonth.parse(shixny, formatter);
                if ((!previousYearMonth.isBefore(sxnyYM)) && (!shixnyYM.isBefore(previousYearMonth))) {
                    faIdSet.add(id);
                }
            }
            baseBean.writeLog("countAgainCommission faIdSet:" + faIdSet);
            if (CollectionUtils.isEmpty(faIdSet)) {
                result.put("mes", "没有需要回算的方案！");
                result.put("code", "400");
                return result;
            }

            RecordSet rs1 = new RecordSet();
            Map<String, String> oldDataMap = new HashMap<>();
            Map<String, String> oldDataDfMap = new HashMap<>();
            String sql1 = "SELECT * FROM uf_yjtcjs WHERE 1 = 1 ";
            rs.execute(sql1);
            while (rs.next()) {
                String id = rs.getString("id");
                String khfa = rs.getString("tcfa");
                String yg = rs.getString("yg");
                String szny = rs.getString("szny");
                String df = rs.getString("df");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(khfa) || StringUtils.isEmpty(yg) || StringUtils.isEmpty(szny)) {
                    continue;
                }
                oldDataMap.put(yg + "_" + khfa + "_" + szny, id);
                oldDataDfMap.put(yg + "_" + khfa + "_" + szny, df);
            }
            Map<String, List<Map<String, String>>> dt1ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt1");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String pjqjkszl = rs.getString("pjqjkszl");
                String pjqjjszl = rs.getString("pjqjjszl");
                String tcxszl = rs.getString("tcxszl");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("pjqjkszl", pjqjkszl);
                map.put("pjqjjszl", pjqjjszl);
                map.put("tcxszl", tcxszl);
                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt1ListMap)) && dt1ListMap.containsKey(mainid)) {
                    mapList = dt1ListMap.get(mainid);
                }
                mapList.add(map);
                dt1ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt2ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt2");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String pjqjkspt = rs.getString("pjqjkspt");
                String pjqjjspt = rs.getString("pjqjjspt");
                String tcxspt = rs.getString("tcxspt");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("pjqjkspt", pjqjkspt);
                map.put("pjqjjspt", pjqjjspt);
                map.put("tcxspt", tcxspt);
                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt2ListMap)) && dt2ListMap.containsKey(mainid)) {
                    mapList = dt2ListMap.get(mainid);
                }
                mapList.add(map);
                dt2ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt3ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt3");
            while (rs.next()) {
                String dtId = rs.getString("id");
                String mainid = rs.getString("mainid");
                String fgslbjl = rs.getString("fgslbjl");
                String jebzjl = rs.getString("jebzjl");
                String dbrs = rs.getString("dbrs");
                String dbjljl = rs.getString("dbjljl");
                String yxj = rs.getString("yxj");
                if (StringUtils.isEmpty(mainid) || StringUtils.isEmpty(dtId)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("dtId", dtId);
                map.put("fgslbjl", fgslbjl);
                map.put("jebzjl", jebzjl);
                map.put("dbrs", dbrs);
                map.put("dbjljl", dbjljl);
                map.put("yxj", yxj);
                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt3ListMap)) && dt3ListMap.containsKey(mainid)) {
                    mapList = dt3ListMap.get(mainid);
                }
                mapList.add(map);
                dt3ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt4ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt4");
            while (rs.next()) {
                String dtId = rs.getString("id");
                String mainid = rs.getString("mainid");
                String fgslbzj = rs.getString("fgslbzj");
                String jebzzj = rs.getString("jebzzj");
                String dbtds = rs.getString("dbtds");
                String dbjlzj = rs.getString("dbjlzj");
                String yxj = rs.getString("yxj");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("dtId", dtId);
                map.put("fgslbzj", fgslbzj);
                map.put("jebzzj", jebzzj);
                map.put("dbtds", dbtds);
                map.put("dbjlzj", dbjlzj);
                map.put("yxj", yxj);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt4ListMap)) && dt4ListMap.containsKey(mainid)) {
                    mapList = dt4ListMap.get(mainid);
                }
                mapList.add(map);
                dt4ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt5ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt5");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String jebzfs = rs.getString("jebzfs");
                String dbfss = rs.getString("dbfss");
                String dbjlfs = rs.getString("dbjlfs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("jebzfs", jebzfs);
                map.put("dbfss", dbfss);
                map.put("dbjlfs", dbjlfs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt5ListMap)) && dt5ListMap.containsKey(mainid)) {
                    mapList = dt5ListMap.get(mainid);
                }
                mapList.add(map);
                dt5ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt6ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt6");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String fgslbmb = rs.getString("fgslbmb");
                String jebzmb = rs.getString("jebzmb");
                String dbjlmb = rs.getString("dbjlmb");
                String yxj = rs.getString("yxj");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("fgslbmb", fgslbmb);
                map.put("jebzmb", jebzmb);
                map.put("dbjlmb", dbjlmb);
                map.put("yxj", yxj);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt6ListMap)) && dt6ListMap.containsKey(mainid)) {
                    mapList = dt6ListMap.get(mainid);
                }
                mapList.add(map);
                dt6ListMap.put(mainid, mapList);
            }
            Map<String, List<Map<String, String>>> dt7ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt7");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String mczh = rs.getString("mczh");
                String jezh = rs.getString("jezh");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("mczh", mczh);
                map.put("jezh", jezh);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt7ListMap)) && dt7ListMap.containsKey(mainid)) {
                    mapList = dt7ListMap.get(mainid);
                }
                mapList.add(map);
                dt7ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt8ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt8");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String fslb = rs.getString("fslb");
                String dbjl = rs.getString("dbjl");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("dbjl", dbjl);
                map.put("fslb", fslb);
                map.put("yxj", yxj);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt8ListMap)) && dt8ListMap.containsKey(mainid)) {
                    mapList = dt8ListMap.get(mainid);
                }
                mapList.add(map);
                dt8ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt9ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt9");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String fslb = rs.getString("fslb");
                String wcz = rs.getString("wcz");
                String jjxs = rs.getString("jjxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("wcz", wcz);
                map.put("fslb", fslb);
                map.put("yxj", yxj);
                map.put("jjxs", jjxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt9ListMap)) && dt9ListMap.containsKey(mainid)) {
                    mapList = dt9ListMap.get(mainid);
                }
                mapList.add(map);
                dt9ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt10ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt10");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String khbms = rs.getString("khbms");
                String dyxs = rs.getString("dyxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("khbms", khbms);
                map.put("dyxs", dyxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt10ListMap)) && dt10ListMap.containsKey(mainid)) {
                    mapList = dt10ListMap.get(mainid);
                }
                mapList.add(map);
                dt10ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt11ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt11");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String pjmc = rs.getString("pjmc");
                String pjqjks = rs.getString("pjqjks");
                String pjqjjsh = rs.getString("pjqjjsh");
                String tcxs = rs.getString("tcxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("yxj", yxj);
                map.put("pjmc", pjmc);
                map.put("pjqjks", pjqjks);
                map.put("pjqjjsh", pjqjjsh);
                map.put("tcxs", tcxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt11ListMap)) && dt11ListMap.containsKey(mainid)) {
                    mapList = dt11ListMap.get(mainid);
                }
                mapList.add(map);
                dt11ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt12ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt12");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String dyfbbhxj = rs.getString("dyfbbhxj");
                String fgqyssbl = rs.getString("fgqyssbl");
                String jlrbl = rs.getString("jlrbl");
                String jlrwfssbl = rs.getString("jlrwfssbl");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("yxj", yxj);
                map.put("dyfbbhxj", dyfbbhxj);
                map.put("fgqyssbl", fgqyssbl);
                map.put("jlrbl", jlrbl);
                map.put("jlrwfssbl", jlrwfssbl);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt12ListMap)) && dt12ListMap.containsKey(mainid)) {
                    mapList = dt12ListMap.get(mainid);
                }
                mapList.add(map);
                dt12ListMap.put(mainid, mapList);
            }

            Map<String, List<Map<String, String>>> dt13ListMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_tcfasz_dt13");
            while (rs.next()) {
                String mainid = rs.getString("mainid");
                String yxj = rs.getString("yxj");
                String pjqjks = rs.getString("pjqjks");
                String pjqjjs = rs.getString("pjqjjs");
                String tcxs = rs.getString("tcxs");
                if (StringUtils.isEmpty(mainid)) {
                    continue;
                }
                Map<String, String> map = new HashMap<>();
                map.put("yxj", yxj);
                map.put("pjqjks", pjqjks);
                map.put("pjqjjs", pjqjjs);
                map.put("tcxs", tcxs);

                List<Map<String, String>> mapList = new ArrayList<>();
                if ((!CollectionUtils.isEmpty(dt13ListMap)) && dt13ListMap.containsKey(mainid)) {
                    mapList = dt13ListMap.get(mainid);
                }
                mapList.add(map);
                dt13ListMap.put(mainid, mapList);
            }

            // 获取分部对应的所有上级分部
            Map<String, String> fbAndSupMap = new HashMap<>();
            rs.execute("SELECT id, supsubcomid FROM hrmsubcompany WHERE canceled != 1 or canceled is null");
            while (rs.next()) {
                String fbId = rs.getString("id");
                if (StringUtils.isEmpty(fbId)) {
                    continue;
                }
                String supsubcomid = rs.getString("supsubcomid");
                fbAndSupMap.put(fbId, supsubcomid);
            }
            Map<String, Set<String>> fbAndXjListMap = new HashMap<>();
            for (Map.Entry<String, String> entry : fbAndSupMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (StringUtils.isNotEmpty(key)) {
                    Set<String> tempSet = new HashSet<>();
                    if ((!CollectionUtils.isEmpty(fbAndXjListMap)) && fbAndXjListMap.containsKey(key)) {
                        tempSet = fbAndXjListMap.get(key);
                    }
                    tempSet.add(key);
                    fbAndXjListMap.put(key, tempSet);
                }
                if (StringUtils.isNotEmpty(value)) {
                    Set<String> tempSet = new HashSet<>();
                    if ((!CollectionUtils.isEmpty(fbAndXjListMap)) && fbAndXjListMap.containsKey(value)) {
                        tempSet = fbAndXjListMap.get(value);
                    }
                    tempSet.add(value);
                    tempSet.add(key);
                    fbAndXjListMap.put(value, tempSet);
                }
            }

            // 获取公司对应部门数
            Map<String, Map<String, String>> gsAndBmsMapMap = new HashMap<>();
            rs.execute("SELECT szny, dyfs, khbms FROM uf_khbms");
            while (rs1.next()) {
                String dyfsTemp = rs.getString("dyfs");
                String sznyTemp = rs.getString("szny");
                if (StringUtils.isEmpty(dyfsTemp) || StringUtils.isEmpty(sznyTemp)) {
                    continue;
                }
                String khbmsTemp = rs.getString("khbms");
                Map<String, String> gsAndBmsMap = new HashMap<>();
                if ((!CollectionUtils.isEmpty(gsAndBmsMapMap)) && gsAndBmsMapMap.containsKey(sznyTemp)) {
                    gsAndBmsMap = gsAndBmsMapMap.get(sznyTemp);
                }
                gsAndBmsMap.put(dyfsTemp, khbmsTemp);
                gsAndBmsMapMap.put(sznyTemp, gsAndBmsMap);
            }

            // 获取扶贫金
            Map<String, String> fpjMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_fpj ");
            while (rs.next()) {
                String yg = rs.getString("yg");
                String sztcfa = rs.getString("sztcfa");
                String sznyFpj = rs.getString("szny");
                String jeFpj = rs.getString("je");
                if (StringUtils.isEmpty(yg) || StringUtils.isEmpty(sztcfa) || StringUtils.isEmpty(sznyFpj)) {
                    continue;
                }
                fpjMap.put(yg + "_" + sztcfa + "_" + sznyFpj, jeFpj);
            }

            // 区域招商经理服务部门数及业绩比例
            Map<String, String> qyzsjlbmyjMap = new HashMap<>();
            Map<String, String> qyzsjlbmyjBlMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_qyzsjlfwbmsyjbl ");
            while (rs.next()) {
                String yg = rs.getString("yg");
                String sztcfa = rs.getString("szkhfa");
                String sznyBmyj = rs.getString("szny");
                String fwbms = rs.getString("fwbms");
                String fsthssbl = rs.getString("fsthssbl");
                if (StringUtils.isEmpty(yg) || StringUtils.isEmpty(sztcfa) || StringUtils.isEmpty(sznyBmyj)) {
                    continue;
                }
                qyzsjlbmyjMap.put(yg + "_" + sztcfa + "_" + sznyBmyj, fwbms);
                qyzsjlbmyjBlMap.put(yg + "_" + sztcfa + "_" + sznyBmyj, fsthssbl);
            }

            // “净新签实收完成”指标编码
            String jxqsswcIndexCode = rs.getPropValue("qz_tc_zm_prop", "jxqsswc_index_code");
            // 获取岗位名称为“销售经理”的人员id
            // 岗位fieldId 3
            String gwFieldId = rs.getPropValue("qz_tc_zm_prop", "gw_field_id_tc");
            Set<String> gwSet = new HashSet<>();
            rs.execute("SELECT b.id,b." + gwFieldId + ",c.gwmc FROM cus_fielddata b LEFT JOIN uf_JCL_GWGL c " +
                    " ON b." + gwFieldId + " = c.id WHERE b.scope = 'HrmCustomFieldByInfoType' AND b.scopeid = 3 AND b." + gwFieldId
                    + " is not null AND c.gwmc = '销售经理'");
            while (rs.next()) {
                String id = rs.getString("id");
                String gwmc = rs.getString("gwmc");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(gwmc) || !StringUtils.equals(gwmc, "销售经理")) {
                    continue;
                }
                gwSet.add(id);
            }

            String today = DateUtil.getCurrentDate();

            // 获取当前生效的分司以及对应级别
            Map<String, String> fbDjMap = new HashMap<>();
            rs.execute("SELECT fb, lb\n" +
                    "FROM uf_yjtl\n" +
                    "WHERE shenxrq <= '" + today + "' AND sxrq >= '" + today + "'\n" +
                    "ORDER BY CONVERT(DATE, shenxrq, 120) DESC");
            while (rs.next()) {
                String fb = rs.getString("fb");
                String lb = rs.getString("lb");
                if (StringUtils.isEmpty(fb) || StringUtils.isEmpty(lb)) {
                    continue;
                }
                fbDjMap.put(fb, lb);
            }

            // 获取BI系统中间表结果映射
            Map<String, String> yjMap = new HashMap<>();
            Map<String, String> fsYjMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_BIxtzjb ");
            while (rs.next()) {
                String yg = rs.getString("yg");
                String gh = rs.getString("gh");
                String szzq = rs.getString("szzq");
                String bizbxm = rs.getString("zbxm");
                if (StringUtils.isEmpty(szzq) || StringUtils.isEmpty(bizbxm)) {
                    continue;
                }
                String wcz = rs.getString("wcz");
                if (StringUtils.isNotEmpty(yg)) {
                    yjMap.put(yg + "_" + bizbxm + "_" + szzq, wcz);
                }
                if (StringUtils.isNotEmpty(gh) && StringUtils.isEmpty(yg)) {
                    fsYjMap.put(gh + "_" + bizbxm + "_" + szzq, wcz);
                }
            }

            // 获取二次折算配置
            Map<String, String> zsxsMap = new HashMap<>();
            rs.execute("SELECT * FROM uf_TCECZSPZ ");
            while (rs.next()) {
                String xm = rs.getString("xm");
                if (StringUtils.isEmpty(xm)) {
                    continue;
                }
                String fadjxs = rs.getString("fadjxs");
                String zjdjxs = rs.getString("zjdjxs");
                String sjdjtds = rs.getString("sjdjtds");
                if (StringUtils.isEmpty(sjdjtds)) {
                    sjdjtds = "0";
                }
                String zsxs = null;
                if (StringUtils.isEmpty(fadjxs) && StringUtils.isEmpty(zjdjxs)) {
                    if (new BigDecimal(sjdjtds).compareTo(new BigDecimal("2")) > 0) {
                        zsxs = "0.85";
                    } else {
                        zsxs = "1";
                    }
                } else {
                    if (StringUtils.isNotEmpty(zjdjxs)) {
                        zsxs = zjdjxs;
                    }
                    if (StringUtils.isNotEmpty(fadjxs)) {
                        zsxs = fadjxs;
                    }
                }
                zsxsMap.put(xm, zsxs);
            }

            List<List> insertList = new ArrayList<>();
            List<List> updateList = new ArrayList<>();

            // 业绩提成建模id
            String modeId = rs.getPropValue("qz_tc_zm_prop", "yjtc_mode_id");
            // 业绩提成查询id
            String cusId = rs.getPropValue("qz_tc_zm_prop", "yjtc_cus_id");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
            SimpleDateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            // 定义一个Comparator来根据Map中key为"pjqjkszl"的value进行排序
            Comparator<Map<String, String>> comparator = Comparator.comparingInt(
                    map -> Integer.parseInt(map.getOrDefault("pjqjkszl", "0")) // 使用getOrDefault来避免NullPointerException
            );

            Comparator<Map<String, String>> comparator1 = Comparator.comparingInt(
                    map -> Integer.parseInt(map.getOrDefault("jebzfs", "0")) // 使用getOrDefault来避免NullPointerException
            );

            Comparator<Map<String, String>> comparator2 = Comparator.comparingInt(
                    map -> Integer.parseInt(map.getOrDefault("yxj", "0")) // 使用getOrDefault来避免NullPointerException
            );

            String sql = "SELECT a.tcfa, a.yg, a.szny, b.fzxsetcbl, b.jslj, b.dqzjljtfzcjrywdqzjljtfzcjrywjl, b.hszb, " +
                    "    b.sfqyeczs, b.sfqytsgz, c.bizbbm, c.bizbmc, c1.bizbbm as lrzbbm, c1.bizbmc as lrzbmc, d.subcompanyid1, " +
                    "    d.departmentid, e." + gwFieldId + ", d.workcode " +
                    " FROM uf_ygtcfa a " +
                    " LEFT JOIN uf_tcfasz b ON a.tcfa = b.id " +
                    " LEFT JOIN uf_bizbys c ON b.hszb = c.id " +
                    " LEFT JOIN uf_bizbys c1 ON b.dqzjljtfzcjrywdqzjljtfzcjrywjl = c1.id " +
                    " LEFT JOIN hrmresource d ON d.id = a.yg " +
                    " LEFT JOIN cus_fielddata e ON e.id = a.yg AND e.scope = 'HrmCustomFieldByInfoType' AND e.scopeid = 3 " +
                    " WHERE 1 = 1 ";
            rs.execute(sql);
            while (rs.next()) {
                String tcfa = rs.getString("tcfa");
                String yg = rs.getString("yg");
                String szny = rs.getString("szny");
                String jslj = rs.getString("jslj");
                String sjlb = rs.getString("hszb");
                String bizbbm = rs.getString("bizbbm");
                String bizbmc = rs.getString("bizbmc");
                String szfb = rs.getString("subcompanyid1");
                String szbm = rs.getString("departmentid");
                String yggh = rs.getString("workcode");
                String szgw = rs.getString(gwFieldId);
                String sfqytsgz = rs.getString("sfqytsgz");
                String sfqyeczs = rs.getString("sfqyeczs");
                String fzxsetcbl = rs.getString("fzxsetcbl");

                String lrzbId = rs.getString("dqzjljtfzcjrywdqzjljtfzcjrywjl");
                String lrzbbm = rs.getString("lrzbbm");
                String lrzbmc = rs.getString("lrzbmc");

                if (StringUtils.isEmpty(tcfa) || StringUtils.isEmpty(yg) || StringUtils.isEmpty(szny) || StringUtils.isEmpty(jslj)
                        || StringUtils.isEmpty(sjlb)) {
                    continue;
                }
                if (!faIdSet.contains(tcfa)) {
                    continue;
                }
                Map<String, String> gsAndBmsMap = gsAndBmsMapMap.get(szny);

                BigDecimal tcBd = new BigDecimal("0");

                String wczjg = null;
                // 增量牌级
                if (StringUtils.equals(jslj, "0")) {
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt1ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String pjqjkszl = mapTemp.get("pjqjkszl");
                        if (StringUtils.isEmpty(pjqjkszl)) {
                            pjqjkszl = "0";
                        }
                        String pjqjjszl = mapTemp.get("pjqjjszl");
                        if (StringUtils.isEmpty(pjqjjszl)) {
                            pjqjjszl = "0";
                        }
                        String tcxszl = mapTemp.get("tcxszl");
                        if (StringUtils.isEmpty(tcxszl)) {
                            tcxszl = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(pjqjkszl)) >= 0 && yjBd.compareTo(new BigDecimal(pjqjjszl)) < 0) {
                            BigDecimal multiply = (yjBd.subtract(new BigDecimal(pjqjkszl))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        } else if (yjBd.compareTo(new BigDecimal(pjqjkszl)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjszl)) >= 0) {
                            BigDecimal multiply = (new BigDecimal(pjqjjszl).subtract(new BigDecimal(pjqjkszl))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        }
                    }
                } else if (StringUtils.equals(jslj, "1")) {   // 普通牌级
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt2ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    for (Map<String, String> mapTemp : mapList) {
                        String pjqjkspt = mapTemp.get("pjqjkspt");
                        if (StringUtils.isEmpty(pjqjkspt)) {
                            pjqjkspt = "0";
                        }
                        String pjqjjspt = mapTemp.get("pjqjjspt");
                        if (StringUtils.isEmpty(pjqjjspt)) {
                            pjqjjspt = "0";
                        }
                        String tcxspt = mapTemp.get("tcxspt");
                        if (StringUtils.isEmpty(tcxspt)) {
                            tcxspt = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(pjqjkspt)) >= 0 && yjBd.compareTo(new BigDecimal(pjqjjspt)) < 0) {
                            tcBd = yjBd.multiply(new BigDecimal(tcxspt));
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "2") || StringUtils.equals(jslj, "3")) {   // 顾问综合排名奖、经理/总监综合排名奖
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt7ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    for (Map<String, String> mapTemp : mapList) {
                        String mczh = mapTemp.get("mczh");
                        if (StringUtils.isEmpty(mczh)) {
                            continue;
                        }
                        String jezh = mapTemp.get("jezh");
                        if (StringUtils.isEmpty(jezh)) {
                            jezh = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(mczh)) == 0) {
                            tcBd = new BigDecimal(jezh);
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "4")) {   // 经理合格奖
                    List<Map<String, String>> mapList = dt3ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    Map<String, String> rsbzMap = new HashMap<>();
                    Map<String, String> jlMap = new HashMap<>();
                    Map<String, String> dbCntMap = new HashMap<>();
                    rs1.execute("select id, subcompanyid1 from hrmresource where managerid =" + yg);
                    while (rs1.next()) {
                        String id = rs1.getString("id");
                        String subcompanyid1 = rs1.getString("subcompanyid1");
                        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(subcompanyid1)) {
                            continue;
                        }
                        String yj = yjMap.get(id + "_" + bizbbm + "_" + szny);
                        if (StringUtils.isEmpty(yj)) {
                            continue;
                        }
                        for (Map<String, String> mapTemp : mapListNew) {
                            String dtId = mapTemp.get("dtId");
                            if (StringUtils.isEmpty(dtId)) {
                                continue;
                            }
                            String fgslbjl = mapTemp.get("fgslbjl");
                            if (StringUtils.isNotEmpty(fgslbjl)) {
                                String djTemp = fbDjMap.get(subcompanyid1);
                                if (StringUtils.isEmpty(djTemp)) {
                                    continue;
                                }
                                if (new BigDecimal(djTemp).compareTo(new BigDecimal(fgslbjl)) != 0) {
                                    continue;
                                }
                            }
                            String jebzjl = mapTemp.get("jebzjl");
                            if (StringUtils.isEmpty(jebzjl)) {
                                jebzjl = "0";
                            }
                            if (new BigDecimal(yj).compareTo(new BigDecimal(jebzjl)) >= 0) {
                                String dbrs = mapTemp.get("dbrs");
                                String dbjljl = mapTemp.get("dbjljl");

                                rsbzMap.put(dtId, dbrs);
                                jlMap.put(dtId, dbjljl);

                                String cntStr = dbCntMap.get(dtId);
                                if (StringUtils.isEmpty(cntStr)) {
                                    cntStr = "0";
                                }
                                dbCntMap.put(dtId, new BigDecimal(cntStr).add(new BigDecimal("1")).toString());
                                break;
                            }
                        }
                    }

                    BigDecimal temp = new BigDecimal("0");
                    for (Map.Entry<String, String> mapTemp : dbCntMap.entrySet()) {
                        String key = mapTemp.getKey();
                        String value = mapTemp.getValue();
                        if (StringUtils.isEmpty(value)) {
                            continue;
                        }
                        BigDecimal valueBd = new BigDecimal(value);
                        if (valueBd.compareTo(BigDecimal.ZERO) == 0) {
                            continue;
                        }
                        String rsbz = rsbzMap.get(key);
                        if (StringUtils.isEmpty(rsbz)) {
                            rsbz = "0";
                        }
                        if (valueBd.compareTo(new BigDecimal(rsbz)) >= 0) {
                            temp = temp.add(valueBd);
                            String jl = jlMap.get(key);
                            if (StringUtils.isEmpty(jl)) {
                                jl = "0";
                            }
                            tcBd = tcBd.add(new BigDecimal(jl));
                        }
                    }
                    wczjg = temp.toString();
                } else if (StringUtils.equals(jslj, "5")) {   // 总监/分总合格奖
                    List<Map<String, String>> mapList = dt4ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    Map<String, String> rsbzMap = new HashMap<>();
                    Map<String, String> jlMap = new HashMap<>();
                    Map<String, String> dbCntMap = new HashMap<>();
                    rs1.execute("select id, subcompanyid1 from hrmresource where managerid =" + yg);
                    while (rs1.next()) {
                        String id = rs1.getString("id");
                        String subcompanyid1 = rs1.getString("subcompanyid1");
                        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(subcompanyid1)) {
                            continue;
                        }
                        String yj = yjMap.get(id + "_" + bizbbm + "_" + szny);
                        if (StringUtils.isEmpty(yj)) {
                            continue;
                        }
                        for (Map<String, String> mapTemp : mapListNew) {
                            String dtId = mapTemp.get("dtId");
                            if (StringUtils.isEmpty(dtId)) {
                                continue;
                            }
                            String fgslbjl = mapTemp.get("fgslbzj");
                            if (StringUtils.isNotEmpty(fgslbjl)) {
                                String djTemp = fbDjMap.get(subcompanyid1);
                                if (StringUtils.isEmpty(djTemp)) {
                                    continue;
                                }
                                if (new BigDecimal(djTemp).compareTo(new BigDecimal(fgslbjl)) != 0) {
                                    continue;
                                }
                            }
                            String jebzjl = mapTemp.get("jebzzj");
                            if (StringUtils.isEmpty(jebzjl)) {
                                jebzjl = "0";
                            }
                            if (new BigDecimal(yj).compareTo(new BigDecimal(jebzjl)) >= 0) {
                                String dbrs = mapTemp.get("dbtds");
                                String dbjljl = mapTemp.get("dbjlzj");

                                rsbzMap.put(dtId, dbrs);
                                jlMap.put(dtId, dbjljl);

                                String cntStr = dbCntMap.get(dtId);
                                if (StringUtils.isEmpty(cntStr)) {
                                    cntStr = "0";
                                }
                                dbCntMap.put(dtId, new BigDecimal(cntStr).add(new BigDecimal("1")).toString());
                                break;
                            }
                        }
                    }

                    BigDecimal temp = new BigDecimal("0");
                    for (Map.Entry<String, String> mapTemp : dbCntMap.entrySet()) {
                        String key = mapTemp.getKey();
                        String value = mapTemp.getValue();
                        if (StringUtils.isEmpty(value)) {
                            continue;
                        }
                        BigDecimal valueBd = new BigDecimal(value);
                        if (valueBd.compareTo(BigDecimal.ZERO) == 0) {
                            continue;
                        }
                        String rsbz = rsbzMap.get(key);
                        if (StringUtils.isEmpty(rsbz)) {
                            rsbz = "0";
                        }
                        if (valueBd.compareTo(new BigDecimal(rsbz)) >= 0) {
                            temp = temp.add(valueBd);
                            String jl = jlMap.get(key);
                            if (StringUtils.isEmpty(jl)) {
                                jl = "0";
                            }
                            tcBd = tcBd.add(new BigDecimal(jl));
                        }
                    }

                    wczjg = temp.toString();
                } else if (StringUtils.equals(jslj, "6")) {   // 盈利分司奖
                    List<Map<String, String>> mapList = dt5ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator1)
                            .collect(Collectors.toList());
                    Map<String, String> dbMap = new HashMap<>();
                    rs1.execute("select a.id,b.bifsbm from \n" +
                            "(select id from hrmsubcompany where (canceled != 1 or canceled is null) and supsubcomid = (select subcompanyid1\n" +
                            "from hrmresource where id = " + yg + ")\n" +
                            "union \n" +
                            "select subcompanyid1 as id\n" +
                            "from hrmresource where id = " + yg + ") a\n" +
                            "left join uf_BIfsyszjb b on a.id = b.dyfb");
                    while (rs1.next()) {
                        String bifsbm = rs1.getString("bifsbm");
                        if (StringUtils.isEmpty(bifsbm)) {
                            continue;
                        }
                        String yj = yjMap.get(bifsbm + "_" + bizbbm + "_" + szny);
                        if (StringUtils.isEmpty(yj)) {
                            continue;
                        }
                        for (int i = mapListNew.size(); i >= 0; i--) {
                            String jebzfs = mapListNew.get(i).get("jebzfs");
                            if (StringUtils.isEmpty(jebzfs)) {
                                continue;
                            }
                            if (new BigDecimal(yj).compareTo(new BigDecimal(jebzfs)) >= 0) {
                                String jl = dbMap.get(jebzfs);
                                if (StringUtils.isEmpty(jl)) {
                                    jl = "0";
                                }
                                dbMap.put(jebzfs, new BigDecimal(jl).add(new BigDecimal("1")).toString());
                                break;
                            }
                        }
                    }
                    BigDecimal temp = new BigDecimal("0");
                    if (!CollectionUtils.isEmpty(dbMap)) {
                        for (Map<String, String> mapTemp : mapListNew) {
                            String jebzfs = mapTemp.get("jebzfs");
                            if (StringUtils.isEmpty(jebzfs)) {
                                continue;
                            }
                            String dbfss = mapTemp.get("dbfss");
                            if (StringUtils.isEmpty(dbfss)) {
                                continue;
                            }
                            String dbjlfs = mapTemp.get("dbjlfs");
                            if (StringUtils.isEmpty(dbjlfs)) {
                                dbjlfs = "0";
                            }
                            String dbrs = dbMap.get(jebzfs);
                            if (StringUtils.isEmpty(dbrs)) {
                                dbrs = "0";
                            }
                            if (new BigDecimal(dbrs).compareTo(new BigDecimal(dbfss)) >= 0) {
                                temp = temp.add(new BigDecimal(dbrs));
                                tcBd = tcBd.add(new BigDecimal(dbjlfs));
                            }
                        }
                    }
                    wczjg = temp.toString();
                } else if (StringUtils.equals(jslj, "7")) {   // 目标奖
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt6ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String fgslbmb = mapTemp.get("fgslbmb");
                        String jebzmb = mapTemp.get("jebzmb");
                        String dbjlmb = mapTemp.get("dbjlmb");
                        if (StringUtils.isNotEmpty(fgslbmb)) {
                            String djTemp = fbDjMap.get(szfb);
                            if (StringUtils.isEmpty(djTemp)) {
                                continue;
                            }
                            if (new BigDecimal(djTemp).compareTo(new BigDecimal(fgslbmb)) != 0) {
                                continue;
                            }
                        }
                        if (StringUtils.isEmpty(jebzmb)) {
                            jebzmb = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(jebzmb)) >= 0) {
                            if (StringUtils.isEmpty(dbjlmb)) {
                                dbjlmb = "0";
                            }
                            tcBd = tcBd.add(new BigDecimal(dbjlmb));
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "8")) {   // 分司补贴
                    bizbmc = "分司类别";
                    List<Map<String, String>> mapList = dt8ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String fslb = mapTemp.get("fslb");
                        String dbjl = mapTemp.get("dbjl");

                        String djTemp = fbDjMap.get(szfb);
                        if (StringUtils.isNotEmpty(fslb)) {
                            if (StringUtils.isEmpty(djTemp)) {
                                continue;
                            }
                            if (new BigDecimal(djTemp).compareTo(new BigDecimal(fslb)) != 0) {
                                continue;
                            }
                        }
                        if (StringUtils.isEmpty(dbjl)) {
                            dbjl = "0";
                        }
                        tcBd = tcBd.add(new BigDecimal(dbjl));
                        if (StringUtils.equals(djTemp, "0")) {
                            wczjg = "A+";
                        } else if (StringUtils.equals(djTemp, "1")) {
                            wczjg = "A";
                        } else if (StringUtils.equals(djTemp, "2")) {
                            wczjg = "B";
                        } else if (StringUtils.equals(djTemp, "3")) {
                            wczjg = "C";
                        } else if (StringUtils.equals(djTemp, "4")) {
                            wczjg = "D";
                        }
                        break;
                    }
                } else if (StringUtils.equals(jslj, "9")) {   // 目标奖_净新签实收
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt9ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String fslb = mapTemp.get("fslb");
                        String wczT = mapTemp.get("wcz");
                        String jjxs = mapTemp.get("jjxs");
                        if (StringUtils.isNotEmpty(fslb)) {
                            String djTemp = fbDjMap.get(szfb);
                            if (StringUtils.isEmpty(djTemp)) {
                                continue;
                            }
                            if (new BigDecimal(djTemp).compareTo(new BigDecimal(fslb)) != 0) {
                                continue;
                            }
                        }
                        if (StringUtils.isEmpty(wczT)) {
                            wczT = "0";
                        }
                        if (yjBd.compareTo(new BigDecimal(wczT)) >= 0) {
                            if (StringUtils.isEmpty(jjxs)) {
                                jjxs = "0";
                            }
                            // 获取“净新签实收完成”指标得的值
                            String jxqssWcz = yjMap.get(yg + "_" + jxqsswcIndexCode + "_" + szny);
                            if (StringUtils.isEmpty(jxqssWcz)) {
                                jxqssWcz = "0";
                            }
                            tcBd = new BigDecimal(jxqssWcz).multiply(new BigDecimal(jjxs));
                            if (tcBd.compareTo(new BigDecimal("3000")) < 0 && new BigDecimal(jjxs).compareTo(BigDecimal.ZERO) > 0) {
                                tcBd = new BigDecimal("3000");
                            }
                            break;
                        }
                    }
                } else if (StringUtils.equals(jslj, "10")) {   // 分总销售额提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt10ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    String bms = gsAndBmsMap.get(szfb);
                    if (StringUtils.isEmpty(bms)) {
                        continue;
                    }
                    String bmxsT = "0";
                    for (Map<String, String> mapTemp : mapList) {
                        String khbmsTemp = mapTemp.get("khbms");
                        if (StringUtils.isEmpty(khbmsTemp)) {
                            continue;
                        }
                        String dyxsTemp = mapTemp.get("dyxs");
                        if (new BigDecimal(bms).compareTo(new BigDecimal(khbmsTemp)) == 0) {
                            bmxsT = dyxsTemp;
                            break;
                        }
                    }
                    if (StringUtils.isEmpty(bmxsT)) {
                        bmxsT = "0";
                    }
                    if (StringUtils.isEmpty(fzxsetcbl)) {
                        fzxsetcbl = "0";
                    }
                    tcBd = yjBd.multiply(new BigDecimal(fzxsetcbl)).multiply(new BigDecimal(bmxsT));
                } else if (StringUtils.equals(jslj, "11")) {   // 分总净利润提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        continue;
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    List<Map<String, String>> mapList = dt11ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    String bms = gsAndBmsMap.get(szfb);
                    if (StringUtils.isEmpty(bms)) {
                        continue;
                    }
                    String bmxsT = "0";
                    List<Map<String, String>> mapBmxsList = dt10ListMap.get(tcfa);
                    for (Map<String, String> mapTemp : mapBmxsList) {
                        String khbmsTemp = mapTemp.get("khbms");
                        if (StringUtils.isEmpty(khbmsTemp)) {
                            continue;
                        }
                        String dyxsTemp = mapTemp.get("dyxs");
                        if (new BigDecimal(bms).compareTo(new BigDecimal(khbmsTemp)) == 0) {
                            bmxsT = dyxsTemp;
                            break;
                        }
                    }
                    if (StringUtils.isEmpty(bmxsT)) {
                        bmxsT = "0";
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String pjqjks = mapTemp.get("pjqjks");
                        if (StringUtils.isEmpty(pjqjks)) {
                            pjqjks = "0";
                        }
                        String pjqjjsh = mapTemp.get("pjqjjsh");
                        if (StringUtils.isEmpty(pjqjjsh)) {
                            pjqjjsh = "0";
                        }
                        String tcxszl = mapTemp.get("tcxs");
                        if (StringUtils.isEmpty(tcxszl)) {
                            tcxszl = "0";
                        }

                        if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjsh)) <= 0) {
                            BigDecimal multiply = (yjBd.subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        } else if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjsh)) > 0) {
                            BigDecimal multiply = (new BigDecimal(pjqjjsh).subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        }
                    }
                    tcBd = tcBd.multiply(new BigDecimal(bms)).multiply(new BigDecimal(bmxsT));
                } else if (StringUtils.equals(jslj, "12")) {   // 大区总经理/集团副总裁(金融业务)提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        yj = "0";
                    }
                    wczjg = yj;
                    BigDecimal yjBd = new BigDecimal(yj);
                    String jlryj = yjMap.get(yg + "_" + lrzbbm + "_" + szny);
                    if (StringUtils.isEmpty(jlryj)) {
                        jlryj = "0";
                    }
                    BigDecimal jlryjBd = new BigDecimal(jlryj);
                    List<Map<String, String>> mapList = dt12ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    String fpj = fpjMap.get(yg + "_" + tcfa + "_" + szny);
                    if (StringUtils.isEmpty(fpj)) {
                        fpj = "0";
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String dyfbbhxjTemp = mapTemp.get("dyfbbhxj");
                        if (StringUtils.isEmpty(dyfbbhxjTemp)) {
                            continue;
                        }
                        Set<String> xjfbSet = fbAndXjListMap.get(dyfbbhxjTemp);
                        if (CollectionUtils.isEmpty(xjfbSet) || !xjfbSet.contains(szfb)) {
                            continue;
                        }
                        String fgqyssblTemp = mapTemp.get("fgqyssbl");
                        if (StringUtils.isEmpty(fgqyssblTemp)) {
                            fgqyssblTemp = "0";
                        }
                        String jlrblTemp = mapTemp.get("jlrbl");
                        if (StringUtils.isEmpty(jlrblTemp)) {
                            jlrblTemp = "0";
                        }
                        String jlrwfssblTemp = mapTemp.get("jlrwfssbl");
                        if (StringUtils.isEmpty(jlrwfssblTemp)) {
                            jlrwfssblTemp = "0";
                        }
                        if (jlryjBd.compareTo(BigDecimal.ZERO) < 0) {
                            BigDecimal fgqyssBd = yjBd.multiply(new BigDecimal(fgqyssblTemp));
                            BigDecimal jlrBd = jlryjBd.multiply(new BigDecimal(jlrwfssblTemp));
                            tcBd = fgqyssBd.add(jlrBd).add(new BigDecimal(fpj));
                        } else {
                            BigDecimal fgqyssBd = yjBd.multiply(new BigDecimal(fgqyssblTemp));
                            BigDecimal jlrBd = jlryjBd.multiply(new BigDecimal(jlrblTemp));
                            tcBd = fgqyssBd.add(jlrBd).add(new BigDecimal(fpj));
                        }
                        break;
                    }
                } else if (StringUtils.equals(jslj, "13")) {   // 区域招商经理提成
                    String yj = yjMap.get(yg + "_" + bizbbm + "_" + szny);
                    if (StringUtils.isEmpty(yj)) {
                        yj = "0";
                    }
                    wczjg = yj;
                    BigDecimal yjBdTemp = new BigDecimal(yj);
                    String qyzsjlbm = qyzsjlbmyjMap.get(yg + "_" + tcfa + "_" + szny);
                    String qyzsjlBl = qyzsjlbmyjBlMap.get(yg + "_" + tcfa + "_" + szny);
                    if (StringUtils.isEmpty(qyzsjlBl)) {
                        qyzsjlBl = "0";
                    }
                    BigDecimal yjBd = BigDecimal.ZERO;
                    if (StringUtils.isNotEmpty(qyzsjlbm) && new BigDecimal(qyzsjlbm).compareTo(BigDecimal.ZERO) != 0) {
                        yjBd = yjBdTemp.multiply(new BigDecimal(qyzsjlBl)).divide(new BigDecimal(qyzsjlbm), 6, RoundingMode.HALF_UP);
                    }

                    List<Map<String, String>> mapList = dt13ListMap.get(tcfa);
                    if (CollectionUtils.isEmpty(mapList)) {
                        continue;
                    }
                    List<Map<String, String>> mapListNew = mapList.stream()
                            .sorted(comparator2)
                            .collect(Collectors.toList());
                    for (Map<String, String> mapTemp : mapListNew) {
                        String pjqjks = mapTemp.get("pjqjks");
                        if (StringUtils.isEmpty(pjqjks)) {
                            pjqjks = "0";
                        }
                        String pjqjjs = mapTemp.get("pjqjjs");
                        if (StringUtils.isEmpty(pjqjjs)) {
                            pjqjjs = "0";
                        }
                        String tcxszl = mapTemp.get("tcxs");
                        if (StringUtils.isEmpty(tcxszl)) {
                            tcxszl = "0";
                        }

                        if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjs)) <= 0) {
                            BigDecimal multiply = (yjBd.subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        } else if (yjBd.compareTo(new BigDecimal(pjqjks)) > 0 && yjBd.compareTo(new BigDecimal(pjqjjs)) > 0) {
                            BigDecimal multiply = (new BigDecimal(pjqjjs).subtract(new BigDecimal(pjqjks))).multiply(new BigDecimal(tcxszl));
                            tcBd = tcBd.add(multiply);
                        }
                    }
                }

                if (StringUtils.equals(sfqytsgz, "0") && gwSet.contains(yg)) {
                    String ksrq = szny + "-01";
                    String jsrq = szny + "-31";
                    String cnt1 = "0";
                    rs1.execute("select count(*) as cnt\n" +
                            "from kq_format_total a\n" +
                            "Left join hrmresource b on b.id = a.resourceid\n" +
                            "LEFT JOIN KQ_HolidaySet c on a.kqdate = c.holidayDate and a.groupId = c.groupId\n" +
                            "WHERE b.managerid = " + yg + " and (c.changeType = 1 or c.changeType = 2) and a.kqdate >= '"
                            + ksrq + "' and a.kqdate <= '" + jsrq + "'");
                    if (rs1.next()) {
                        cnt1 = rs1.getString("cnt");
                    }
                    if (StringUtils.isNotEmpty(cnt1) && new BigDecimal(cnt1).compareTo(BigDecimal.ZERO) > 0) {
                        rs1.execute("select count(*) as cnt \n" +
                                " from kq_format_total a \n" +
                                " LEFT JOIN KQ_HolidaySet c on a.kqdate = c.holidayDate and a.groupId = c.groupId\n" +
                                " WHERE a.resourceid = " + yg + " and (c.changeType = 1 or c.changeType = 2) and a.kqdate >= '"
                                + ksrq + "' and a.kqdate <= '" + jsrq + "'");
                        if (rs1.next()) {
                            String cnt2 = rs1.getString("cnt");
                            if (StringUtils.isNotEmpty(cnt2) && new BigDecimal(cnt2).compareTo(BigDecimal.ZERO) > 0
                                    && new BigDecimal(cnt1).divide(new BigDecimal(cnt2), 2, RoundingMode.HALF_UP).compareTo(new BigDecimal("8")) > 0) {
                                tcBd = tcBd.multiply(new BigDecimal("0.7"));
                            }
                        }
                    }
                }

                if (StringUtils.isNotEmpty(sfqyeczs) && StringUtils.equals(sfqyeczs, "0")) {
                    String zsxs = zsxsMap.get(yg);
                    if (StringUtils.isNotEmpty(zsxs)) {
                        tcBd = tcBd.multiply(new BigDecimal(zsxs));
                    }
                }

                String key = yg + "_" + tcfa + "_" + szny;
                String oldDataId = oldDataMap.get(key);
                String oldDataDf = oldDataDfMap.get(key);
                if (StringUtils.isEmpty(oldDataDf)) {
                    oldDataDf = "0";
                }
                BigDecimal hsbcBd = tcBd.subtract(new BigDecimal(oldDataDf));
                if (StringUtils.isNotEmpty(oldDataId)) {
                    List rowParam = new ArrayList<>();
                    rowParam.add(bizbbm);
                    rowParam.add(bizbmc);
                    rowParam.add(wczjg);
                    rowParam.add(tcBd.toString());
                    rowParam.add(hsbcBd.toString());
                    rowParam.add(dtFormat.format(new Date()));
                    rowParam.add(oldDataId);
                    updateList.add(rowParam);
                }
            }

            if (CollectionUtils.isEmpty(updateList)) {
                baseBean.writeLog("countAgainCommission updateList is null.");
            } else {
                baseBean.writeLog("countAgainCommission updateList size is:" + updateList.size());
                rs.executeBatchSql("UPDATE uf_yjtcjs SET hswczcode = ?, hswczmc = ?, hswczjg = ?, hstcjg = ?, hsbc = ?, zxhssj = ? WHERE id = ?", updateList);
            }
        } catch (Exception e) {
            baseBean.writeLog("countAgainCommission error:" + e);
        }
        return result;
    }

    @Override
    public Map<String, Object> getEndWorkDate(Map<String, Object> params) {
        baseBean.writeLog("getEndWorkDate start.");
        Map<String, Object> result = new HashMap<>();
        baseBean.writeLog("getEndWorkDate params: " + params);
        String resourceId = Util.null2String(params.get("resourceId"));//申请人
        String kqDate = Util.null2String(params.get("kqDate"));//申请日期
        String offsetStr = "1";//允许的偏移量

        if ( org.apache.commons.lang.StringUtils.isBlank(resourceId)) {
            result.put("code",500);
            result.put("msg","请先选择人员");
            return result;
        }
        if ( org.apache.commons.lang.StringUtils.isBlank(kqDate)) {
            result.put("code",500);
            result.put("msg","请先选择申请日期");
            return result;
        }
        if ( org.apache.commons.lang.StringUtils.isBlank(offsetStr)) {
            result.put("code",500);
            result.put("msg","请先设置偏移量");
            return result;
        }

        Integer offset = Util.getIntValue(offsetStr);
        baseBean.writeLog("offset: " + offset);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start = LocalDate.parse(kqDate, formatter);

        LocalDate dateTemp = start;
        boolean isGoing = true;
        int index = 0;
        for (int i = 0; ; i++) {
            if (index == offset) {
                break;
            }
            dateTemp = start.plusDays(i);
            boolean holiday = KQHolidaySetBiz.isHoliday(resourceId, dateTemp.format(formatter), true);
            if (!holiday) {
                index = index + 1;
            }
        }

        String endDate = dateTemp.format(formatter);
        result.put("code", 200);
        result.put("msg", "请求成功");
        result.put("data", endDate);
        return result;
    }
}
